14 Dec Tuesday Python Homework 18:30 – 22.12.13.
Being able to send encoded messages during World War II was very important to the Allies. The messages were always sent after being encoded with a known password. Having a fixed password was of course insecure, thus there was a need to change it frequently. However, a mechanism was necessary to send the new password. One of the mathematicians working in the cryptographic team had a clever idea that was to send the password hidden within the message itself. The interesting point was that the receiver of the message only had to know the size of the password and then search for the password within the received text.
A password with size N can be found by searching the text for the most frequent substring with N characters. After finding the password, all the substrings that coincide with the password are removed from the encoded text. Now, the password can be used to decode the message.
Your mission has been simplified as you are only requested to write a program that, given the size of the password and the encoded message, determines the password following the strategy given above.
To illustrate your task, consider the following example in which the password size is three (N = 3) and the text message is just ‘baababacb’. The password would then be aba because this is the substring with size 3 that appears most often in the whole text (it appears twice) while the other six different substrings appear only once (baa ; aab ; bab ; bac ; acb).
The input file contains several test cases, each of them consists of one line with the size of the password, 0 < N ≤ 10, followed by the text representing the encoded message. To simplify things, you can assume that the text only includes lower case letters.
For each test case, your program should print as output a line with the password string.
Sample Input 3 baababacb Sample Output aba
Ken is working for the secret agency. He is going to send a secret message that encodes UPPERCASE words by shifting their letters forward.
Shifting a letter by S positions means to go forward S letters in the alphabet. For example, shifting B by S = 3 positions gives E. However, sometimes this makes us go past Z, the last letter of the alphabet. Whenever this happens we wrap around, treating A as the letter that follows Z. For example, shifting Z by S = 2 positions gives B.
Ken’s code depends on a parameter K and also varies depending on the position of each letter in the word. For the letter at position P, they use the shift value of S = 2*K+3*P.
For example, here is how “FIND” is encoded when K = 3. The first letter F has a shift value of S = 2 × 3 + 3 × 1 = 9; it wraps around and becomes the letter O. The second letter, I, has S = 2 × 3 + 3 × 2 = 12 and becomes U. The last two letters become C and V. So Ken sends the secret message: OUCV
Write a program to encode messages sent by Ken.
The input will be two lines. The first line will contain the positive integer K (K < 10), which is used to compute the shift value. The second line of input will be the original word, which will be a sequence of uppercase characters of length at most 20.
The output will be the encoded word of uppercase letters.
Sample Input 1 3 FIND Output for Sample Input 1 OUCV Sample Input 2 5 MISSILE Output for Sample Input 2 ZYLOHNJ
Today in gym class, your class decided to play dodgeball, and Jack is selected as one of the team captains! N students are lined up in a row, waiting to be put on teams. Jack is allowed to pick multiple students as long as they are adjacent to one another, and the first letter of each of their names are the same (case insensitive).
As an assistant to Jack, help him decide what his first choice should be by writing a program to determine all the possible choices he could make!
The first line contains the integer N (1≤N≤105), representing the total number of students.
The second line contains N space separated strings, each representing the name of the ith student in line. Each name will be no longer than 20 characters and only contain letters from the English alphabet.
Output one integer, representing the total number of choices Jack can make.
Sample Input 5 Sarah Timmy Turner Betty Bob Sample Output 7 Explanation for Sample Input The possible groups of people that aurpine could choose are: Sarah Timmy Turner Betty Bob Timmy, Turner Betty, Bob