Project Programs Final
Project Programs Final
According to Wikipedia, ROT13 ("rotate by 13 places") is a simple Caesar cipher used for obscuring
text by replacing each letter with the letter thirteen places down the alphabet. A becomes N, B
becomes O, and so on up to M, which becomes Z, then the sequence reverses: N becomes A, O
becomes B, and so on to Z, which becomes M.
The algorithm is used in online forums as a means of hiding joke punchlines, puzzle solutions, movie
and story spoilers, and offensive materials from the casual glance.
A noticeable feature of this cipher is that it is symmetrical; that is, to undo ROT13, the same
algorithm is applied, so the same code can be used for encoding and decoding. Your program must be
able to apply the ROT13 cipher to its input, and output the result. Only upper case letters A through Z
need to be considered, and spaces must be left untouched.
Example 1:
Input string: EXAMPLE ONE
Output string: RKNZCYR BAR
Example 2:
Input string: ENIVAR
Output string: RAVINE
Problem 2:
Create a program that is able to spell out numbers from 1 to 999999 that are entered in
numeric form.
Example 1:
Input number: 40
Output string: forty
Example 2:
Input number: 2007
Output string: two thousand seven
Problem 3:
This problem considers a list consisting of three lines of three words, and a single search
word to lookup within the list. The objective is to output the location of the first occurrence
of the single search word within the three lines. This location must include the line number,
word number, and character number (the single word may appear as part of another word as
in Example 1).
If there are multiple occurrences of the search word, the program should only print the first
occurrence.
You may assume that the search word occurs at least once in the list of words.
The lookup must be case sensitive. For instance, “lookup,” “LOOKUP,” and “Lookup” are
considered different words.
Example 1:
Enter three lines of three words:
Happy Fortieth Anniversary
to UAB CIS
two thousand seven
Enter a single work to lookup:
sand
“sand” found at line 3, word 2, character 5
Example 2:
Enter three lines of three words:
pink blue red
purple yellow green
red yellow blue
Enter a single work to lookup:
red
“red” found at line 1, word 3, character 1
Problem 4:
Roman numerals use the following symbols:
I (one)
V (five)
X (ten)
L (fifty)
C (one hundred)
D (five hundred)
M (one thousand)
These are usually written in linear order (e.g., I, II, III) with smaller numerals always
following larger numerals except when they are to be subtracted (e.g., IV means 5 - 1).
Subtractions are used instead of writing four consecutive occurrences of the same symbol
(i.e., IV should be used instead of IIII, MCM instead of MDCCCC, etc.). Only symbols
which are powers of 10 may be used in subtractions (i.e., I, X, C, and M). Furthermore,
subtraction rules require that a symbol representing 10^x may not precede any symbol larger
than 10^(x+1) (e.g., IC is not permitted to represent 99 but XC is 90).
Write a program to input a Roman numeral and output the decimal equivalent if the Roman
numeral is well formed and “syntax error” otherwise. Assume that no number will be larger
than MMMCMXCIX (3,999).
Example 1:
Input: MMVII
Output: 2007
Example 2:
Input: MIM
Output: syntax error
Problem 5:
DNA is a two-stranded molecule. Each strand is a polynucleotide composed of A
(adenosine), T (thymidine), C (cytidine), and G (guanosine) residues. The two strands of
DNA run antiparallel (i.e., at each nucleotide residue along the double-stranded DNA
molecule, the nucleotides are complementary). That is, A forms two hydrogen-bonds with T;
C forms three hydrogen bonds with G. In most cases the two-stranded, antiparallel,
complementary DNA molecule folds to form a helical structure, which resembles a spiral
staircase. This is the reason why DNA has been referred to as the “Double Helix.” An
example of two complementary strands of DNA would be:
ATGGAATTCTCGCTC
TACCTTAAGAGCGAG
This genetic sequencing problem is to find complementary matching strands of DNA.
Write a program that reads two strings, and outputs: 1) the beginning part of the second
strand which does not match, 2) the part of the second strand which the first string matches,
enclosed in -'s, and 3) the remaining part of the second strand which does not match. Note
that if there is no match, the second strand will be output with no –'s. If there are multiple
matching strings, outputting only the first match is sufficient.
Example 1:
Enter 1st strand: ATGC
Enter 2nd strand: ATGCATACGA
Answer: ATGCA-TACG-A
Example 2:
Enter 1st strand: GAATTC
Enter 2nd strand: TGGAATTCT
Answer: TGGAATTCT
Problem 6:
The Luhn algorithm, also known as the "modulus 10" or "mod 10" algorithm, is a simple
checksum formula used to validate a variety of identification numbers, such as credit card
numbers. The formula verifies a number against its included check digit, which is usually
appended to a partial account number to generate the full account number. This account
number must pass the following test:
1. Counting from rightmost digit (which is the check digit) and moving left, double the value
of every even-positioned digit. For any digits that thus become 10 or more, take the two
numbers and add them together. For example, 1111 becomes 2121, while 8763 becomes
7733 (from 2×6=12 → 1+2=3 and 2×8=16 → 1+6=7).
2. Add all these digits together. For example, if 1111 becomes 2121, then 2+1+2+1 is 6; and
8763 becomes 7733, so 7+7+3+3 is 20.
3. If the total ends in 0 (put another way, if the total modulus 10 is congruent to 0), then the
number is valid according to the Luhn formula; else it is not valid. So, 1111 is not valid (as
shown above, it comes out to 6), while 8763 is valid (as shown above, it comes out to 20).
Create a program that takes as input a single number sequence (each sequence has at least 2
numbers and no more than 10 numbers). Determine if the number is correct according to the
Luhn algorithm.
Example 1:
Input number: 446667652
Output: Number is not correct
Example 2:
Input number: 446667651
Output: Number is correct
Problem 7:
Pascal’s Triangle is constructed by forming a new digit as the sum of the digits above it in
the triangle. The first few lines are:
1
11
121
1331
Mr. Pascal had a cousin named Bob. Bob wasn’t as mathematically oriented as his famous
kinsman, but Bob wanted a triangle of his own. Your task is to determine the pattern for
Bob’s triangle (understanding the pattern is a key to your solution), then write a program that
can generate it.
Allow the user to input the number of rows of Bob’s triangle to generate, and then display
those requested number of rows, Your output may be left-aligned, as shown below.
Example 1:
Number of rows: 5
Output:
1
11
21
1211
111221
Example 2:
Number of rows: 6
Output:
1
11
21
1211
111221
312211
Problem 8:
While your professor, Dr. Jacobi, is a brilliant mathematician, he has often expressed his
frustrations with spending his limited free time having to grade assignments. For his online
courses, the university has generously provided him with Chalkboard TM , which will allow
him to create quizzes and homework assignments for his students to complete online. He can
then download their answers for manual grading. Unfortunately, the designers of this system
didn’t think to include a grading system in their product!
Dr. Jacobi, knowing that you’re a whiz with computers, has agreed to give you bonus points
if you can automate his grading by calculating the class average for the assignment so he can
get back to proving theorems.
Input:
Each test case will consist of several lines of input: a line indicating N, the number of
students in the class, a line containing the answer key as a list of comma separated values,
and N lines containing a comma separated list of student answers, where each line is one
student. You can assume all questions are weighted equally with a perfect exam being worth
100 points, all student solutions have the same number of questions answered as the key, and
that blank answers are denoted by a *. All quizzes will have at least one question.
Output:
Print out the class average as a percentage, flooring (discarding the decimal portion) to the
nearest whole number. Do not do any rounding before printing the output.
Sample Inputs:
3
A,B,C,D
A,*,C,D
A,B,C,D
*,*,*,*
5
1,2,A,F
1,2,B,F
1,2,B,*
1,2,A,F
4
1,2,A,F
1,2,A,F
Sample Outputs:
58
85
5
Problem 9:
Thomas wants to find out how many days there are until Christmas. Help him write a
program that can tell
him how many days until Christmas on a certain date.
Input
All inputs will come from standard in (console keyboard). The first value is an integer that
represents the
number of data sets to follow. Each data set will contain a month, date and year each
separated by a space.
Output
All output goes to standard out (console window). Your program should produce n lines of
output, n representing the number of data sets. Each line should print out the number of days
until Christmas formatted as seen below in the sample output.
Assumptions you can make:
• All given dates will be in the same year.
• If the given date occurs after Christmas, then you must find out how many days there are
until next year’s Christmas.
• If the day occurs on Christmas then the number of days until Christmas is 0.
• Christmas is on December 25th.
Sample Input
4
10 8 2002
1 5 2002
6 24 2002
12 26 2002
Sample Output
78 DAYS UNTIL CHRISTMAS
354 DAYS UNTIL CHRISTMAS
184 DAYS UNTIL CHRISTMAS
364 DAYS UNTIL CHRISTMAS
Problem 10:
Given a matrix (two-dimensional array) of positive integers, traverse the elements in a
clockwise-spiral fashion and output the elements in clockwise-spiral order. For example, if
this is the input matrix:
1234
5678
9 10 11 12
13 14 15 16
Then the matrix should be traversed as such:
And the output should be this: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11
10
The first line of the input contains a number representing the number of rows in the matrix,
the second line of the input contains a number representing the number of columns in the
matrix, and there are several lines afterward that contain a space-separated list of positive
integers corresponding to the rows of the input matrix. The output should be a space-
separated list of positive integers, corresponding the the elements of the matrix when it is
spirally traversed. Input and output processing has been handled in the sample code, if you
wish to use it.
Sample Input
4
4
1234
5678
9 10 11 12
13 14 15 16
Sample Output
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Problem 11:
Caesar's cipher is one of the earliest encryption techniques. A message is encrypted by replacing each
character with another character in the alphabet. An encoding with a _xed displacement n replaces
each character with the character + n in the same alphabet (rollover at the end of the alphabet). One
way to attack an encrypted message is to map the words in the message to a dictionary. If a consistent
mapping with a _xed size n can be found, the encryption can be broken.
Implement a decipher that breaks messages encoded with Caesar's cipher. The program _rst reads a
line containing a dictionary of words separated by blanks, followed by a line containing N, the
number of test cases, followed by N encrypted lines (in all lower case). Your program should use the
provided dictionary and attempt to map each line's words to words in the provided dictionary.
If successful, the program prints the number n indicating character displacement used when the
message was originally encrypted, followed by a blank and the decoded message for each of the N
lines. If decryption fails, print 1.
Sample Input/Output
Sample Input
alabama blue birmingham color story street world
5
zcdlatsvt iwpi lxaa rwpcvt ndjg ldgas.
uyggv jqog cncdcoc.
dolyl aol zrplz hyl zv isbl.
blue street
oplioi iahkajs jaashsshh
Expected Output
15 knowledge that will change your world.
2 sweet home alabama.
7 where the skies are so blue.
0 blue street
-1
5
Problem 12:
Computer scientists have a strange way of looking at arithmetic expressions. What a regular person
sees as 3+4*5 looks like + 3 * 4 5 to a computer scientist. The former is called the infix notation,
while the latter is called the prefix notation, or the Polish notation. In the Polish notation, the operator
(e.g. +) comes first, followed by the left-hand side sub-expression, followed by a right-hand side
subexpression. Each of the subexpressions again is in the prefix notation. One benefit is that no
brackets are needed, they can be inferred: + 3 * 4 5 can be unambiguously understood as + 3 (* 4 5),
that is, as 3+(4*5).
You task is to write a converter from prefix notation to infix notation for expressions that involve
numbers in the range 0-9 (that is, every number is a single digit, there won’t be numbers such as 123),
as well as two binary operators: + and *. The converter should preserve the left-right order, that is, +
3 4 is translated to 3+4 and not to 4+3. To simplify things, every binary operation should be enclosed
in brackets: instead of 3+4*5 you should print (3+(4*5)).
Input
A string of length up to 100 characters representing an expression in prefix notation. The string will
contain only the following characters: 0123456789+* and each character will be separated from the
next by a single white space.
Output
A string representing the expression in infix notation. Do not use any white spaces.
Sample Input
+*3++4567
Sample Output
((3*((4+5)+6))+7)
Problem 13:
Alice and Bob went to a cryptography seminar, and learned a trick to protect their personal
communication, named ``One-Time Pad''. The main idea is that, given a sequence of numbers (a pad),
any message (plaintext) can be proven to be securely encrypted into a new message (ciphertext) if
each plaintext character is encoded into a ciphertext character by a number from the pad, if each
number from the pad is only used once.
Alice then decides to use the one-time pad to encrypt her messages for Bob by shifting each letter in
the plaintext message by k positions later in the alphabet, where k is determined by the one-time pad.
Shifts occur in alphabetical order, between the letters ``a'' and ``z''. If a letter is shifted past ``z'', it
starts back at ``a" and continues shifting. For example, shifting a letter by k = 25, the plaintext ``c'' is
transformed into a ciphertext ``b'', and similarly, shifting by k= 2, the plaintext ``z'' is shifted to ``b''.
Input
The input consists of four parts:
1) start with the size of the one-time pad;
2) then followed by a sequence of numbers for the pad;
3) then followed by a series of words for the plaintext;
4) and finally terminated by a line containing only -1.
You may assume that the maximum size of the pad is 100 numbers, all numbers in the pad are non-
negative integers below 1000, and that all input plaintext words will be lowercase letters.
Output
For each plaintext to be encrypted, output a line containing the corresponding ciphertext.
Sample Input
5
12345
bb zzz
-1
Sample Output
cd cde
Problem 14:
The famous Goldbach's conjecture, a well-known unsolved problem in number theory, states that
every even integer greater than 2 can be written as a sum of two prime numbers. For nearly 300 years,
even the best mathematician has not been able to crack it.
Let (𝑁) be the number of different ways to represent a number N as a sum of two prime numbers. The
Goldbach's conjecture can be stated as follows: (𝑁) > 0 for all even 𝑁≥4. For example, G(4) = 1 and
G(18) = 2.
Given an integer 𝑁, your task is to write a program to compute F(N), the sum of G for all even
numbers from 4 up to 2𝑁:
(𝑁) = (4) + ...+ 𝐺(2𝑁)
Input
The first line of the input file contains a number 1≤𝐾≤20. The following 𝐾 lines contains 𝐾 integers
𝑁1,2,…,𝑁𝑘, one per line, where (3 ≤ 𝑁𝑖 ≤ 500000) for 𝑖=1..𝐾.
Output
For each 𝑁𝑖 output on a separate line the value of (𝑁𝑖).
Sample Input
2
9
7
Sample Output
12
8
Problem 15:
An eccentric old philosopher and philanthropist has decided to give his money away using a game of
his own devising. People buy a number and are placed in the lottery system in the order in which they
purchase a ticket. People may choose what number they like, but their ordering in the system is given
exactly by the time at which they buy the ticket. No two tickets can be sold at the same time, but
many people may choose the same number. Ticket sales stop after 100,000 tickets have been sold.
Three winners (a winning set) are chosen from the list. Here are the criteria for being chosen: The
first person in a winning set of three has both a lower number and bought their ticket before the
second person in the winning set. The second person in the winning set has both a lower number and
bought their ticket before the third person in the winning set.
For a given sale of tickets you must identify how many possible winning sets there are for that sale.
Input
The first line will tell you how many people bought tickets during this sale.
The second line will have one integer for each person, separated by spaces, in the order that they
bought their ticket.
Output
A single integer telling how many winning sets of three exist for this ticket sale.
Sample Input
5
12 23 11 25 37
Sample Output
5
Problem 16:
The botchagaloop value of a number x is found as follows. First, convert x to base 8.
Call this p. Next, sort the digits of p in increasing order. Call this q. Subtract q from p (in
base 8,
of course). Repeat the “sort-subtract” sequence 4 more times, or until the digits in the result
are
in sorted order (whichever come first). Finally, convert the number back to base 10.
For example, 3418 has a botchagaloop value of 1008. It is computed as follows: 3418 =
65328;
6532-2356 = 4154; 4154-1445 = 2507; 2507-257 = 2230; 2230-223 = 2005; 2005-25 = 1760;
and finally, 17608 = 1008. Note that there is at least one subtraction and at most 5
subtractions.
There will be 5 inputs. Each is a positive integer less than 1,000,000. Print the botchagaloop
value of each input.
Sample Input:
Line #1: 3418
Line #2: 123
:::
Sample Output:
Output #1: 1008
Output #2: 28
:::
Test Input:
Line #1: 251
Line #2: 5049
Line #3: 14711
Line #4: 23328
Line #5: 199697
Test Output:
Output #1: 28
Output #2: 301
Output #3: 0
Output #4: 11193
Output #5: 95844
Problem 17:
Remember those “brain puzzlers" of the form:
In case you don't remember them, the problem is pretty straightforward: Find numbers to
replace the A, B, C,… that make the equation true. The replacements must be consistent; that
is, replace all A's by one digit, all B's by a different digit, and so on. For example, the
equation 1538+583 = 2121 solves the equation above. The equation has 5 more solutions:
1547+574 = 2121, 1574+547 = 2121, 1583 + 538 = 2121, 3658 + 685 = 4343, and 3685 +
658 = 4343. The second sample problem below has three solutions: 546 + 54 = 600, 576 + 57
= 633, and 586 + 58 = 644.
There will be 10 inputs. Each input consists of three strings, p, q, and r. The strings are
composed of the letters A through Z and numbers. You need to report the number of
solutions there are to the problem p + q = r. The leading digit in each term cannot be a 0.
Also, to keep the running time of this program reasonable, we promise that there won't be
more than 4 different letters in the original data.
Sample Input:
...
Sample Output:
Output #1: 7 (solutions are: 11+89, 22+78, 33+67, 44+56, 66+34, 77+23, 88+12)
...
Test Input:
Test Output:
Output #1: 1
Output #2: 5
Output #3: 3
Output #4: 4
Output #5: 7
Output #6: 20
Output #7: 4
Output #8: 1
Output #9: 32
Output #10: 14
Problem 18:
Due to a terrible accident involving a bottle of shampoo and a vial of acid, your friend's skull
has melted, revealing his brain. It turns out, however, that this didn't hurt him at all, as he is a
robot. His brain is a Pentium 166 running Linux. As he explains the intricacies of being a
robot, you wonder how many other people you know are actually robots. Listening carefully,
you hear the key that you need to discern who is a robot and who isn't: robots have glitches in
their speech systems.
If there are eight consecutive words in a sentence that each contain an “a” or an “e” (or an
“A” or an “E”), then the eighth word is reversed. The capitalization is retained, however, so
any capitalized letters in the word remain capitalized when the word is reversed.
If the sentence has exactly one word, the robots always say "Wheebop!" instead of the
sentence.
If the sentence has more than 10 words, the 11th word is replaced with the original first
word of the sentence, the 12th word is replaced with the original second word, the 13th word is
replaced with the original third word, the 21st word is replaced by the original 11th word, etc.
The Problem:
Your program is to read in a file of normal text, and print it out as a robot would say it. For
the purposes of this problem, a "word" is defined as a sequence of characters separated by a
space, an end-of-line, or a sentence-ender (see below).
A sentence is defined as zero or more words ended by a sentence-ender. The sentence-enders
are either a period (.), exclamation (!), or question mark (?). These punctuation marks always
indicate the end of a sentence, except for a few special cases of the period: "Mr.", "Mrs.",
"Ms.", "Dr.", "Sr.", and "Jr." are considered words (if they are followed by a space, an end-
of-line, or sentence-ender), and the period that ends the abbreviation is not considered an end
of the sentence.
The Input:
The input will be a file of text. There will be no more than 70 characters per line of input. No
sentence will contain more than 100 words. There will be no tab characters in the input file.
The Output:
The output should be the input, with the above rules applied. Do not remove excess white
space or carriage returns; the text should be exactly the same as the input except for the
application of the above rules.
Sample Input:
These are the times that trie mens' feet, but not very much.
"Mom," asked Bobby Joe, "Why do you hate me so?"
"Sleep, you
foolish little boy, you are a monster and you’re Dr. Talon!" said
Mom.
Sample Output:
These are the times that trie mens' ,teef but not These are.
"Mom," asked Bobby Joe, "Why do you hate me so?"
"Sleep, you
foolish little boy, you are a monster " "Sleep, you foolish!"
said Mom.
Problem 19:
Johnny sat down one summer morning to a bowl of alphabet soup (you thought I was going
to say cereal, right?). He began playing with the letters, spelling the names of his favorite
fruits like ORANGE and APPLE and BANANA. Then he noticed that some of the letters in
the name of each type of fruit had loops in them—a kind of “fruit loops.”
The letter B has two loops, the letters A, D, O, P, Q, and R have one loop each, and the
letters C,
E, F, G, H, I, J, K, L, M, N, S, T, U, V, W, X, Y, Z have no loops.
The Problem:
Given some letters that supposedly represent the name of some kind of fruit, count the
number of loops in that name.
The Input:
The first line of the input file will contain a single positive integer, N, which represents the
number of test cases in that input file. N will be less than 200. Each of the next N lines of the
input file will contain a test case. A test case will consist only of a single “fruit” name--a
sequence of one to twelve uppercase letters.
The Output:
For each test case in the input, output a single line that follows the following format:
where fruit-name is the input sequence of letters and number is the total number of loops in
those
same letters.
Sample Input:
ORANGE
BANANA
LETTUCE
PLUM
Sample Output:
Problem 20:
Bill and Ted love to watch movies. They go to the theater every Friday night to see that
week’s blockbuster. They also have a few restaurants where they like to eat dinner on movie
night. Unfortunately neither of them ever have the foresight to check the movie times or plan
for the waiting time at the restaurant, so they never know how much time they have to get to
the movie after eating dinner.
The Problem:
Since Bill and Ted are tired of getting to the theater twenty minutes after the movie has just
started, they’ve asked you to write them a program to help them out. The program will gather
movie show times and the waiting time at that week’s restaurant of choice, and let them
know how much time they have to get to the earliest showing possible after dinner. It always
takes Bill and Ted 57 minutes to eat. Disregard travel times between work, the restaurant,
and the theater.
The Input:
There will be multiple data sets. The input will start with a positive integer on the first line
representing the number of data sets. Each data set will consist of three lines. The first line of
each data set will be the time Bill and Ted leave work in 24-hour format. The second line
will be a single positive integer representing the waiting time at the restaurant, in minutes.
The third line will contain the show times of the movie in 24-hour format, each separated by
a single space. Movie times will always be confined to the same 24-hour interval. The first
movie of the day will not start before 11:00 am (11:00) or after 11:00 pm (23:00). Movie
times will be in
chronological order. There will always be at least one movie that Bill and Ted can make. No
input line will be longer than 70 characters.
The Output:
For each data set, print a single line telling Bill and Ted the time of the earliest showing they
can make followed by a comma, a space, and how much time they have to get there.
Additionally, if they have less than twenty minutes to make it, print another comma, space,
and the message “hurry up!” at the end of the line. Leave one blank line after each output
line. See the Sample Output for the correct format.
Sample Input:
4
17:00
35
13:00 15:30 18:00 20:30 23:00 1:30
20:30
60
12:30 15:00 16:30 17:40 19:10 20:20 21:50 22:40 0:10 1:40
18:02
31
13:00 15:30 18:00 20:30 23:00 1:30
17:07
85
12:30 15:30 18:30 21:30 0:30
Sample Output:
20:30, you have 1 hour and 58 minutes
22:40, you have 13 minutes, hurry up!
19:30, you have 1 hour
21:30, you have 2 hours and 1 minute
General Instructions for the Project
Each program should be well documented, supported by a proper alogorithm, and meaning
and legible screenshots of the programs outputs. Write a variable glossary for each of the
programs in the format as shown below:
Font Size : 12
Margins:
Inside : 3cm
OutSide ; 2 cm
Top : 2 cm
Bottom : 2 cm