Monday, September 9, 2013

Crime Scene Investigation

     In my Advanced Java Concepts class we were given a project to recover pictures from a .raw file. In this file it contained pictures that had been deleted. So it was our job to recover the pictures and then after recovering them go to the spot around school and take a picture at least one of the spots. To complete the assignment we had to submit our code, as well as, the picture that we took at one of the spots. The specifics of the assignment are included in the text below:


The Task

     A digital camera has been found, but there do not appear to be any pictures on it. Perhaps they have been deleted. Of course, the camera's flash card has data on it, even if the data is not readily accessible. The contents of the camera's flash card is given to you to see if you can recover any of the pictures.
Your task is to write a Java program to recover as many pictures as possible from the flash card data 80MB. Take a picture of yourself at any one of the scenes found in the pictures.

     You need to know that all pictures taken by this camera are jpeg images. Also, jpeg viewing software does not care if a jpeg file has extraneous bytes at the end of the file because the size of the image data is encoded in the jpeg format. You are asked to recover each jpeg picture in a file of its own, but extraneous bytes at the end of the file are allowed. As a consequence it is not necessary to actually write a program that fully parses the jpeg format (this is quite difficult).

     This camera formats the flash card in blocks of 512 bytes. Each picture the camera takes starts at the begining of a block and is stored in contiguous blocks. If the picture does not fit exactly into an even number of blocks (and it hardly ever would), the rest of the last block is not used. Deleting a picture marks the picture's blocks as available to be reused. Hopefully, no pictures have been deleted from the flash card before another picture was taken. If this is the case, none of the blocks have been used to store more than one picture since the flash card was new.

Note

     My camera broke and now I have a different camera. Try to make you program detect any jpg image (assuming the camera manufactor follows the jpg standard).

Input/Output

     The program should expect one command-line argument --- the URL of the (binary) flash card image. So, the program might be invoked as follows:
java Recover http://www.cs.fit.edu/~ryan/cse4051/projects/csi/card.raw 
or:
java Recover file://test.bin

     The size of input will be a multiple of the block size. For each jpg image identified in the input file, write a separate file: image01.jpgimage02.jpgimage03.jpg, etc. Please use names of this form; start numbering with 1; use 2 decimal digits. Do not output thumbnail images (or any other suparts) separately.



      This was a challenging project as we were given information that was very broad and were told that the jpeg images link above contained some information we could use. It turns out that the only information that was useful was the beginning indicator of the jpeg.


      The easiest way to solve this problem is by looking for the beginning indicator and then searching for the next beginning indicator. Now you are probably asking, "Well what the heck Zach, what about the ending indicator? You have to know when the picture ends." That is a valid question, but because the way an operating system interprets the bytes of a jpeg image, you do not have to worry about he extraneous bytes after the end of image indicator. The picture viewer will ignore these extraneous bytes and display the image.

     Another useful piece of information is that the flash card is formatted in 512 bytes, and by only reading 512 blocks at a time it makes it easier to interpret the data.


     Originally when doing this assignment I did not have the information that I provided above and was iterating through the whole 512 byte block that I had taken in from the file. Later, I found out that if the begin of image indicator is not at the beginning of the block then there is no need to search the rest of the block. This effectively took my searching algorithm from O(N) to O(1), where N is the number of bytes per file because even though you check only a block at a time you have to search the whole card to make sure that there are no more pictures, which in the algorithm performance world is fantastic. Also, because we only have to check the first two bytes it went from O(N + 512) to O(N + 2) where N is the number of bytes and the constant following is the number of bytes searched. Even though this does not save very much time in the grand scheme of things it is still helpful to optimize as much as possible.


     I did not include the method that saves the picture to the file but that can be found on my GitHub. The pictures found throughout this post are some of the pictures that I recovered and I will include the one that I submitted. Overall it was an interesting assignment and can be done in very little code.


Friday, September 6, 2013

Propositional Checker


     This is a project that was given to me by Dr. Stansifer, my professor for Advanced Java Concepts. The project description and goals go as follows:

The Task



     Check a propositional formula with up to 6 distinct propositional letters written in postfix notation. Use a stack (ArrayDeque) of BitSet.

     Propositionals are one letter names from a-z, but at most 6 distinct propositions will appear in the formula. The connectives are:
- (unary) negation
& (binary) conjunction
| (binary) disjunction
= (binary) equivalence
> (binary) implication

Sample Input and Output

Input:
p
p-p|
p -p&
pq&
pq=
p q >

Output:
contingent
tautology
contradiction
contingent
contingent
contingent



     For each line of input print, print one of the three possibilities. The second line, for example: "not p or p" is a tautology.

     I recommend using nextLine() from the Scanner class to the whole line. Don't forget to ignore any white space in the line.

     I will include portions of my code but the rest can be found on my GitHub.
<!-- --!>
     The portion of the program that I think is the most important for efficiency is the bit shift operation used to calculate the power of two. This is a common practice but saves time because the Math.pow can be very expensive especially for larger powers of two.

     The next important piece that helps tremendously is storing all of the propositional characters and the BitSet values related to each value in a HashMap. Doing this will create a table similar to:

Propositional Character Bit Values
P 0101
Q 0011

     Of course in this program with the maximum bits being 64 it would display all 64 in the same pattern shown above. With these two tools this project became simple and noticing the pattern of each variable as it is added to the table was important, as well.

Sunday, September 1, 2013

TJU Programming Contest



     This is a compilation of TJU Programming problems that were given to us from Dr. Stansifer. The last question gave me the most trouble, as I forgot to make sure that the difference and sum could be equal.



Head or Tail

     John and Mary have been friends since nursery school. Since then, they have shared a playful routine: every time they meet, they play Head or Tail with a coin, and whoever wins has the priviledge of deciding what they are going to play during the day. Mary always choose Head, and John always choose Tail.

     Nowadays they are in college, but continue being truly good friends. Whenever they meet, they still play Head and Tail, and the winner decides which film to watch, or which restaurant to have dinner together, and so on.

     Yesterday Mary confided to John that she has being keeping a record of the results of every play since they started, in nursery school. It came as a surprise to John! But since John is studying Computer Science, he decided it was a good opportunity to show Mary his skills in programming, by writing a program to determine the number of times each of them won the game over the years.

Input

     The input contains several test cases. The first line of a test case contains a single integer N indicating the number of games played (1 ≤ N ≤ 10000). The following line contains N integers Ri, separated by space, describing the list of results. If Ri = 0 it means Mary won the ith game, if Ri = 1 it means John won theith game (1 ≤ i ≤ N). The end of input is indicated by N = 0.

Output

     For each test case in the input your program should output a line containing the sentence "Mary won X times and John won Y times", where X ≥ 0 and Y ≥ 0.

Sample Input

5
0 0 1 0 1
6 0 0 0 0 0 1
0

Output for the Sample Input

Mary won 3 times and John won 2 times 
Mary won 5 times and John won 1 times





Number Sequence


     Given a positive integer number, we want to generate a number sequence with the following rules:

     If the current number is 1, the process will be terminated. Otherwise, if the current number is even, the next number will be n/2. If the current number is odd (except 1), the next number will be 3*n+1. Then we turn to deal with the new number, until we get 1.

     For example, given the first number 22, we will get {22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1}. The length of this sequence is 16.

     Given the first number, your task is to find the length of the sequence generated with these rules. We assume that the length will not be more than 1000.

Input

There is only one number N (1 ≤ N ≤ 1000) for each test case, indicating the first number of the sequence.
The input is terminated by a zero.

Output

Output one number in a line for each test case, indicating the length of the sequence we generate.

Sample Input

22
1000
1
0

Sample Output

16
112
1

Author: RoBa




Bovine Latin

     The cows have heard that the pigs use a secret language called "Pig Latin" when they want to communicate with each other without Farmer John being able to understand what they are saying. Thinking this is an excellent idea, they have invented their own version, aptly called Bovine Latin.

     Converting an English word to a Bovine Latin word is quite simple. For words that start with a vowel ('a', 'e', 'i', 'o' or 'u'), "cow" is added to the end of the word; for example, "udder" becomes "uddercow". For words that do not begin with a vowel, the first letter is moved to the end of the word, and "ow" is added; e.g., "farmer" becomes "armerfow". So "the cows escape at dawn" becomes "hetow owscow escapecow atcow awndow." The cows fervently believe that FJ will not understand this subterfuge.

     Never known as enthusiastic linguists, the cows find this translation quite tedious and thus have asked you to write a program that will take single words and translate them into Bovine Latin. They will provide you with N (1 ≤ N ≤ 100) words to translate; word lengths range from 3 to 40 letters.

Input

* Line 1: A single integer: N
* Lines 2..N + 1: One word per line.

Output

* Lines 1..N: The Bovine Latin translations of the given words

Sample Input

5
udder
farmer
milk
aaa
zzz


Sample Output

uddercow 
armerfow 
ilkmow 
aaacow 
zzzow




Speed Limit

     Bill and Ted are taking a road trip. But the odometer in their car is broken, so they don't know how many miles they have driven. Fortunately, Bill has a working stopwatch, so they can record their speed and the total time they have driven. Unfortunately, their record keeping strategy is a little odd, so they need help computing the total distance driven. You are to write a program to do this computation.

For example, if their log shows
Speed in miles per hourTotal elapsed time in hours
202
306
107
this means they drove 2 hours at 20 miles per hour, then 6-2=4 hours at 30 miles per hour, then 7-6=1 hour at 10 miles per hour. The distance driven is then (2)(20) + (4)(30) + (1)(10) = 40 + 120 + 10 = 170 miles. Note that the total elapsed time is always since the beginning of the trip, not since the previous entry in their log.

Input: The input consists of one or more data sets. Each set starts with a line containing an integer n, 1 ≤ n ≤ 10,  followed by n pairs of values, one pair per line. The first value in a pair, s, is the speed in miles per hour and the second value, t, is the total elapsed time. Both s and t are integers, 1 ≤ s ≤ 90 and 1 ≤ t≤ 12.  The values for are always in strictly increasing order. A value of -1 for n signals the end of the input.

Output: For each input set, print the distance driven, followed by a space, followed by the word "miles". 

Example input:Example output:
3
20 2
30 6
10 7
2
60 1
30 5
4
15 1
25 2
30 3
10 5
-1
170 miles
180 miles
90 miles




Above Average

     It is said that 90% of frosh expect to be above average in their class. You are to provide a reality check.

     The first line of standard input contains an integer C, the number of test cases. C data sets follow. Each data set begins with an integer, N, the number of people in the class (1 ≤ N ≤ 1000). N integers follow, separated by spaces or newlines, each giving the final grade (an integer between 0 and 100) of a student in the class. For each case you are to output a line giving the percentage of students whose grade is above average, rounded to 3 decimal places.

Sample Input

5
5 50 50 70 80 100
7 100 95 90 80 70 60 50
3 70 90 80
3 70 90 81
9 100 99 98 97 96 95 94 93 91

Output for Sample Input

40.000%
57.143%
33.333%
66.667%
55.556%




Beat The Spread!

     Superbowl Sunday is nearly here. In order to pass the time waiting for the half-time commercials and wardrobe malfunctions, the local hackers have organized a betting pool on the game. Members place their bets on the sum of the two final scores, or on the absolute difference between the two scores.

     Given the winning numbers for each type of bet, can you deduce the final scores?

     The first line of input contains n, the number of test cases. n lines follow, each representing a test case. Each test case gives s and d, non-negative integers representing the sum and (absolute) difference between the two final scores. For each test case, output a line giving the two final scores, largest first. If there are no such scores, output a line containing "impossible". Recall that football scores are always non-negative integers.

Sample Input

2
40 20
20 40

Output for Sample Input


30 10
impossible



Recaman's Sequence



     This is a problem from a TJU contest that Dr. Stansifer, from FIT, had over the weekend. During the school year this is a regular occurance. Dr. Stansifer will start that programming contest on Saturday at noon and then it will go until close to midnight. This is one of the programs that were available to be solved.


     The Recaman's sequence is defined by a0 = 0 ; for m > 0, am = am-1 - m if the resulting am is positive and not already in the sequence, otherwise am = am-1 + m.
The first few numbers in the Recaman's Sequence is 0, 1, 3, 6, 2, 7, 13, 20, 12, 21, 11, 22, 10, 23, 9... . Given k, your task is to calculate ak.

Input
The input consists of several test cases. Each line of the input contains an integer k where 0 ≤ k ≤ 500000. The last line contains an integer -1, which should not be processed.

Output
For each k given in the input, print one line containing ak to the output.

Sample Input
7
10000
-1
Sample Output
20
18658




     My solution originally to this problem was to precompute the values for myself and then insert the values into an array as literals. However, Java does not like that many literals in a program. So instead I precomputed the values using the formulas above. Then I would take in the input which would then be the index in my array that I needed to reference. The code below gave me a correct result with:

Code Time  Memory
0.8K0'00.12"267412K