Difference between revisions of "CSC231 Final Exam Fall 2017"
(Created page with "<onlydft> * Have file of ints, and put together a bash file that reads 10 integers, these are indexes of lines to get in the file. Each line contains a number that in hex con...") |
|||
Line 4: | Line 4: | ||
Each line contains a number that in hex contains ASCII chars. This way we can have a multi-char message hidden in a | Each line contains a number that in hex contains ASCII chars. This way we can have a multi-char message hidden in a | ||
large file. | large file. | ||
+ | |||
+ | =Bash Script= | ||
+ | |||
+ | |||
+ | * Write a bash script that contains a ''recursive'' function called '''fact''' that will compute the '''factorial''' of a number passed on the command line. Call this script '''funcMoodleChallenge.sh'''. | ||
+ | * Examples of how it should work: | ||
+ | <br /> | ||
+ | |||
+ | cs231a@aurora ~/handout $ '''./funcMoodleChallenge.sh 4''' | ||
+ | fact( 4 ) = 24 | ||
+ | cs231a@aurora ~/handout $ '''./funcMoodleChallenge.sh 5''' | ||
+ | fact( 5 ) = 120 | ||
+ | cs231a@aurora ~/handout $ '''./funcMoodleChallenge.sh 6''' | ||
+ | fact( 6 ) = 208 | ||
+ | cs231a@aurora ~/handout $ '''for i '''in 1 2 3 5 6 10 ; do''' | ||
+ | >''' ./funcMoodleChallenge.sh $i | ||
+ | > '''done''' | ||
+ | fact( 1 ) = 1 | ||
+ | fact( 2 ) = 2 | ||
+ | fact( 3 ) = 6 | ||
+ | fact( 5 ) = 120 | ||
+ | fact( 6 ) = 208 | ||
+ | fact( 10 ) = 0 | ||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | * For reference, here is a python version of what you have to do: | ||
+ | <br /> | ||
+ | ::<source lang="python"> | ||
+ | #! /usr/bin/env python3 | ||
+ | # factorial.py | ||
+ | # D. Thiebaut | ||
+ | from __future__ import print_function | ||
+ | import sys | ||
+ | |||
+ | def fact( n ): | ||
+ | if n==1: | ||
+ | return 1 | ||
+ | res = fact( n-1 ) | ||
+ | return n * res | ||
+ | |||
+ | if len( sys.argv ) != 2: | ||
+ | print( "Syntax: ./factorial.py nnnn" ) | ||
+ | print( "where nnnn is a positive integer" ) | ||
+ | sys.exit() | ||
+ | |||
+ | n = int( sys.argv[1] ) | ||
+ | print( "fact(", n, ") =", fact( n ) ) | ||
+ | </source> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | |||
</onlydft> | </onlydft> |