Courses/CS 460/Fall 2005/Homework/Jeff Bailey/Nov 12

From CSWiki

Jump to: navigation, search

Contents


I am participating in my brother's wedding today so will not be in class.

[edit] DigitsToInt

This was pretty easy for the most part. Strip the final value from the list to simplify the FoldR call. It uses the length of J to determine the power of 10 by which I should be multiplied.

local
   fun {Int Xs}
      Z = {List.last Xs}
      Ys = {List.take Xs {Length Xs}-1}
   in
      {FoldR Ys fun {$ I J} {FD.plus {FD.times I {Pow 10 {Length {IntToString J}}}} J} end Z}
   end
in
   {Browse {Int [3 2 1 0]}}
   
end

[edit] Math002

Every alphabet represents a different digit from 1 to 9 to make the calculation. What are they?

AB * C = DE and DE + FG = HI

Solutions:

  • 17 * 2 = 34 and 34 + 56 = 90
  • 18 * 2 = 36 and 36 + 54 = 90
  • 17 * 4 = 68 and 68 + 25 = 93
  • 38 * 2 = 76 and 76 + 14 = 90
local
   proc {Math Answer}
      A B C D E F G H I
      Vars = [A B C D E F G H I]
   in
      Answer = [A B]#[C]#[D E]#[F G]#[H I]
      Vars ::: 0#9                                      
      {FD.distinct Vars}                                

      A \=: 0                                          
      D \=: 0
      F \=: 0
      H \=: 0

      {Int [A B]} * {Int [C]} =: {Int [D E]}
      {Int [D E]} + {Int [F G]} =: {Int [H I]}
      {FD.distribute ff Vars}
   end
in
   {Browse {SearchAll Math}}
end

[edit] Ooops

OOOP represents a 4 digit number. What is it?

OOOP * P = SOOPO

Solution:

  • 9997
local
   proc {Ooops Answer}
      O P S
      Vars = [O P S]
   in
      Answer = [O O O P]#[P]#[S O O P O]
      Vars ::: 1#9                                      
      {FD.distinct Vars}                                

      {Int [O O O P]} * {Int [P]} =: {Int [S O O P O]}
      {FD.distribute ff Vars}
   end
in
   {Browse {SearchAll Ooops}}
end