Mimir:Draft5 Answer Key

Back to Table of Contents

=Answer Key=

Perl
print "Enter your input:\n"; my $N = ; chomp($N); if ($N % 2 == 0) { print "The number is even\n"; } else { print "The number is odd\n"; }
 * 1) first answer

@words; while (true) { print "Enter a word or enter nothing to stop: "; my $userword = ; chomp $userword; if ($userword eq "") {       foreach (@words) { print "$_\n"; }       last; }   else {       push (@words, $userword); } }
 * 1) second answer

print"Welcome to guess my number!\n"; $compNumber=int rand 20; while(true){ print"Guess a number between 1 and 20: "; chomp($userNumber=); if($compNumber>$userNumber){ print"Your guess was too low, try again.\n"; }       if($compNumber<$userNumber){ print"Your guess was too high, try again.\n"; }       if($compNumber==$userNumber){ print"Well done, $compNumber was my number!"; last; }   } sub fibonacci { my $n = shift;
 * 1) third answer
 * 1) fourth answer

return undef if $n < 0;

my $f; if ($n == 0) { $f = 0; } elsif ($n == 1) { $f = 1; } else { $f = fibonacci($n-1) + fibonacci($n-2); }   return $f; }

sub factorial {  my $number = shift;
 * 1) fifth answer

if ( $number <= 1 ) { return 1; }  else { return $number * factorial( $number - 1 ); } }

print "Type a message to be reversed: \n"; my $str = ; my $rev = reverse $str; print $rev. "\n";
 * 1) sixth answer

Python
print("Enter your input: ") number = input if (int(number) % 2 == 0): print("Number is even") else: print("Number is odd")
 * 1) first answer

words = [] while True: print('Enter word: ' + str(len(words) + 1) +   ' (Or enter nothing to stop.):') name = input if name == '': break words = words + [name] print('The words are:') for name in words: print(' ' + name)
 * 1) second answer

import random
 * 1) third answers

number = random.randint(1, 20) while True: print ("Guess a number between 1 and 20: ") guess = input guess = int(guess) if guess < number: print('Your guess is too low.') if guess > number: print('Your guess is too high.') if guess == number: print ("Well done, " + str(number) + " was my number!") break

def fib(n): if n==1 or n==2: return 1 return fib(n-1)+fib(n-2)
 * 1) fourth answer

def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)
 * 1) fifth answer

print("Type a message to be reversed: ") word = input message = '' for b in word: message = b+message print (message)
 * 1) sixth answer