Post Reply 
Programming puzzles: processing lists!
04-20-2017, 04:09 PM (This post was last modified: 04-20-2017 10:05 PM by Han.)
Post: #5
RE: Programming puzzles: processing lists!
Here are some of my solutions for the HP Prime. I'll post RPL solutions later.

Code:

Spoilers...



















EXPORT LC1(l)
BEGIN
  local s:=size(l);
  local n;
  local e1:=3;
  local e2:=5;

  if l(1)==5 then
    e1:=5;
    e2:=3;
  end;

  for n from 1 to s do
    if bitand(n,1) then
      if l(n)<>e1 then return(0); end;
    else
      if l(n)<>e2 then return(0); end;
    end;
  end;
  return(1);
 
END;

EXPORT LC2(l)
BEGIN
  local s:=size(l);
  local n;
  for n from 1 to s do
    if l(n)==3 then
      l(n):=4;
    else
      if l(n)==5 then l(n):=7; end;
    end;
  end;
  return(l); 
END;

EXPORT LC3(l)
BEGIN
  local s:=size(l);
  local m:=floor(s/3);
  local n;

  makelist(
    l(
      ifte(n<=m,l(s-m+n),l(n-m))
    ), n, 1, s
  );
END;

EXPORT LC4small(l)
BEGIN
  local s:=size(l);
  local j,n,p2;

  n:=0; p2:=1;
  for j from 1 to s do
    if l(j) then n:=n+p2; end;
    p2:=2*p2;
  end;
  n:=n+1;
  s:=s+(bitand(n,p2)<>0);
  p2:=2*p2; 
  
  makelist((bitand(n,p2:=bitsr(p2,1))<>0),j,1,s);

END;

EXPORT LC4(l)
BEGIN
  local s:=size(l);
  local j,d;
  local c:=1;

  for j from s downto 1 do
    d:=l(j)+c;
    c:=bitsr(d,1);
    l(j):=bitand(d,1); 
  end;

  if c then
    return(makelist(ifte(j>1,l(j-1),1),j,1,s+1));
  else
    return(l);
  end;
END;

EXPORT LC5(l)
BEGIN
  local s:=size(l);
  local j,d;
  local c:=0;

  for j from s downto 1 do
    d:=l(j)+c+1;
    c:=bitsr(d,1);
    l(j):=bitand(d,1); 
  end;
END;

EXPORT LC6(l)
BEGIN
  local s:=size(l);
  local j,n;
  local ints:={};
  local count:={};

  for j from 1 to s do
    if n:=pos(ints,l(j)) then
      count(n):=count(n)+1;
      if count(n)>ints(n) then return(0); end;
    else
      ints(0):=l(j);
      count(0):=1;
    end;
  end;

  s:=size(count);
  for j from 2 to s do
    if count(j)<>count(j-1) then return(0); end;
  end;
  return(1);
END;

EXPORT LC7(l)
BEGIN
  local s:=size(l);
  local m:=s/2;
  local n;
  local nl:=makelist(
    ifte(n<=m,l(n),l(n-1)),
    n,1,s+1
  );
  nl(m+1):=-1;
  return(nl);
END;

EXPORT LC8(l)
BEGIN
  local s:=size(l);
  local m:=s/2;
  local j;

  for j from 1 to m do
    if l(j)<>l(m+j) then return(0); end;
  end;
  return(1);
END;

EXPORT LC9(l)
BEGIN
  local s:=size(l);
  local j,k,m;
  local b:=0;
  local n:=3;

  for j from 1 to s do
    if l(j)<>3 then break; end;
  end;

  k:=j-1;
  m:=s/k;
  if m<>floor(m) then return(0); end;

  for j from k+1 to s do
    if b then
      b:=b-1; 
    else
      b:=k;
      if n==3 then n:=5; else n:=3; end;
    end;
    if l(j)<>n then return(0); end;  
  end;
  return(1); 
END;

EXPORT LC10(l)
BEGIN
  local s:=size(l);
  local j,n;
  local ints:={};
  local count:={};

  for j from 1 to s do
    if n:=pos(ints,l(j)) then
      count(n):=count(n)+1;
      if count(n)>ints(n) then return(0); end;
    else
      ints(0):=l(j);
      count(0):=1;
    end;
  end;

  s:=size(count);
  for j from 1 to s do
    if count(j)<>ints(j) then return(0); end;
  end;
  return(1);
END;

EXPORT LC11(l)
BEGIN
  local s:=size(l);
  local m:=bitsr(s,1);
  local j;

  for j from 1 to m do
    if l(j)<>l(s-j+1) then return(0); end;
  end;
  return(1); 
END;

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Programming puzzles: processing lists! - Han - 04-20-2017 04:09 PM



User(s) browsing this thread: 3 Guest(s)