Every other letter - python-2.7

So, I have tried this problem for what it seems like a hundred times this week alone.
It's filling in the blank for the following program...
You entered jackson and ville.
When these are combined, it makes jacksonville.
Taking every other letter gives us jcsnil.
The blanks I have filled are fine, but the rest of the blanks, I can't figure out. Here they are.
x = raw_input("Enter a word: ")
y = raw_input("Enter another word: ")
print("You entered %s and %s." % (x,y))
combined = x + y
print("When these are combined, it makes %s." % combined)
every_other = ""
counter = 0
for __________________ :
if ___________________ :
every_other = every_other + letter
____________
print("Taking every other letter gives us %s." % every_other)
I just need three blanks to this program. This is basic python, so nothing too complicated or something I can match wit the twenty options. Please, I appreciate your help!

The first blank needs to define letter so that each time through the loop it is the letter at position counter in combined
The second blank needs to test for the current letter's position being one that gets included
The last blank needs to modify counter for the next value of letter (much as the initial value of counter was for the first letter).

The solution is to slice with a step value.
In [10]: "jacksonville"[::2]
Out[10]: 'jcsnil'
The slice notation means "take the subset starting at the beginning of the iterable, ending at the end of the iterable, selecting every second element". Remember that Python slices start by selecting the first element available in the slice.
EDIT: Didn't realize it had to fill in the blanks
for letter in combined:
if(counter % 2) == 0:
every_other = every_other + letter
counter += 1
Since taking every other would mean you take every second letter, or every second pass through the loop, and you use counter to track how many passes you've made, you can use modulo division (%) to check when to take a letter. The base case is that 0 % 2 = 0, which lets you take the first letter. It's important to remember to always increment the counter.
A way to do this without the manual counter, which was already mentioned in comments is to use the enumerate function on combined. When given an iterable as a parameter, enumerate returns a generator which yields two values with each request, the position in the iterable, and the value of the iterable at that position.
I'm using this language, talking about iterables as index-able sequences, but it could be any generator-like object which doesn't have to have a finite, pre-defined sequence.

Related

Looking for matches within different lists of different formats using for loops

In Python I am trying to search lists of numbers for a single number contained in these strings.
If the first number (46424409) is contained within any of the total rows, the row containing that particular number should be added to a separate group. After performing this search on the first number, it will do the same for the second number etc.. until there is no match to be found:
Example data:
[46424409,51337324248,5173349797,5179273316,5331867664,5183367665]
[46800409,5113033868,5122425797,51325252716,513333664,51867633365]
[44440409,51730868,5242449797,513533716,51867664,51867665]
[4683300409,5173330868,5173349797,51792716,5111664,51844565]
found3 = []
for num in total_list:
print(num)
for j in row_list:
print(j)
print('row list element')
if num in j:
found3.append(j)
else:
if num not in j:
break
(row_list is a row, total_list is all rows combined)
this works but only if I omit not use the first for loop over the total list, now it goes wrong and I get a repetition of the first row list.
Does anybody know how to integrate the second for loop?

How to store all the inputs in a list using a single command?

I was doing some ds and algo problems from faceprep and a question came:
Amy Farrah Fowler asks her students to give 3 examples for positive odd numbers. When the student specifies a correct answer, his/her score is incremented by 1. When the student specifies a positive even number, his/her score is decremented by 0.5. When the student specifies a negative number, he/she will not be given any more chances to correct his or her mistake and his/her score will be decremented by 1. So a student's turn comes to an end when he/she has correctly specified 3 positive odd numbers or when the student has specified a negative number. Some students didn't know the difference between odd numbers and even numbers and they made many mistakes and so it was difficult for her to maintain the scores. She asks for your help. Can you please help her by writing a program to calculate the score?
INPUT & OUTPUT FORMAT:
Input consists of a list of integers.
The output consists of a single line. The score needs to be corrected to 1 decimal place.
[For this exercise, don't worry about duplicate odd numbers. Even if the students specify the same odd number thrice, it is accepted].
input is given as:
1
2
3
4
5
Now How to take input and store it in a variable by single command?
till now I was doing but now number of inputs are different for each case study.
:
a=int(input())
b=int(input())
c=int(input())
I want to do this using a single command so that each input will store in a list.
Try this code snippet to store the input in the list "List_w".
List_w = []
try:
while(1):
List_w.append(int(input()))
except:
print(List_w)
if your input is separated by white space, you can use:
arr = list(map(int, input().split()))
print(arr) # input 1 2 3 4 # output [1,2,3,4]
if your input is based on given range use:
for _ in range(int(input())):
arr = list(map(int, input().split()))
print(arr)
if the inputs separated by new line you can use append method or one liner:
arr = [int(input()) for _ in range(int(input()))]
print(arr)
# input
3
4
5
6 # output [4, 5, 6]
know more about input here https://www.geeksforgeeks.org/python-get-a-list-as-input-from-user/

Pyspark-length of an element and how to use it later

So I have a dataset of words, I try to keep only those that are longer than 6 characters:
data=dataset.map(lambda word: word,len(word)).filter(len(word)>=6)
When:
print data.take(10)
it returns all of the words, including the first 3, which have length lower than 6. I dont actually want to print them, but to continue working on the data that have length greater than 6.
So when I will have the appropriate dataset, I would like to be able to select the data that I need, for example the ones that have length less than 15 and be able to make computations on them.
Or even to apply a function on the "word".
Any ideas??
What you want is something along this (untested):
data=dataset.map(lambda word: (word,len(word))).filter(lambda t : t[1] >=6)
In the map, you return a tuple of (word, length of word) and the filter will look at the length of word (the l) to take only the (w,l) whose l is greater or equal to 6

Explaining frequency[toupper(new_letter) - 'A']++;

So I've been searching for a solution to a problem that one step involves counting the frequency of each unique letter. Everywhere I go has the same array incrementor. I haven't seen this form and don't fully understand it. I have attempted to find support documentation for the format but can't figure out what it actually does.I Can get it to work; however, I'm not sure what each peice represents.
Peice I'm having issues understanding is what's going on inside the brackets here.
frequency[toupper(new_letter) - 'A']++;
Where frequency is an array
an example from: count number of times a character appears in an array?
Algorithm:
Open file / read a letter.
Search for the letters array for the new letter.
If the new letter exists: increment the frequency slot for
that letter: frequency[toupper(new_letter) - 'A']++; If the new
letter is missing, add to array and set frequency to 1.
After all letters are processed, print out the frequency array: `
cout << 'A' +
index << ": " << frequency[index] << endl;
any help understanding would be much apprecaited.
This is simply an array. Maybe the part that is confusing you is toupper(new_letter) - 'A' what we do here is - we convert the letter to upercase and then subtract the ASCII code of 'A' from the ASCII code of the result. Thus the result is a number in the range [0-25]. After that by adding this to 'A' we get the origianl uppercase character. As for the rest of the algorithm - this is simply something like counting sort.
Unfortunately, this solution is not completely portable. It assumes that in the execution character set, the capital letters A-Z have consecutive values. That is, it assumes 'A' + 1 is equal to 'B', 'B' + 1 is equal to 'C', and so on. This is not necessarily true, but it usually is.
toupper simply converts whatever character is passed to it to uppercase. Subtracting 'A' from this, given the above assumption, will work out the "distance" from 'A' to the given letter. That is, if new_letter is 'A', the result will be 0. If it is 'b', the result will be 1. As you can see, the reason for using toupper was to make it independent as to whether new_letter was uppercase or lowercase.
This result (essentially the position of the letter in the alphabet) is then used to access the array. If frequency is an array of 26 ints (one for each letter), you will access the corresponding int. That int is then incremented.
If it's an array (e.g. int frequency[26];) then we don't add to array - it is already there, but with a value of zero.
The ++ operator is short hand for add one to the thing, so
frequency[toupper(new_letter) - 'A']++;
is the same as:
frequency[toupper(new_letter) - 'A'] = frequency[toupper(new_letter) - 'A'] + 1;
Obviously, the short hand version is much easier to read, as there is much less repetition that has to be carefully checked that it's the same on both sides, etc.
The index is toupper(new_letter) - 'A' - this works by first making any letter into an uppercase one - so we don't care if it's a or A, 'c' or C, etc, and then subtract the value of first letter in the alphabet, 'A'. This means that if new_letter is 'A' the index is zero. If new_letter is 'G' we use index 7, etc. [This assumes that all the letters are sequential, which isn't absolutely certain, and for sure, if we talk about languages other than English that have for example ä, ǹ, Ë or ê, etc as part of the language, then those would definitely not be following A-Z]
If you were to count the number of letters in a piece of text by hand, you could just list all the letters A-Z along the edge of the paper, and then put a dot next to each letter as you read them in the text, and then count the number of dots. This does the same sort of thing, except it keeps each count running as you go along.

Constructing Strings using Regular Expressions and Boolean logic ||

How do I construct strings with exactly one occurrence of 111 from a set E* consisting of all possible combinations of elements in the set {0,1}?
You can generate the set of strings based on following steps:
Some chucks of numbers and their legal position are enumerated:
Start: 110
Must have one, anywhere: 111
Anywhere: 0, 010, 0110
End: 011
Depend on the length of target string (the length should be bigger than 3)
Condition 1: Length = 3 : {111}
Condition 2: 6 > Length > 3 : (Length-3) = 1x + 3y + 4z
For example, if length is 5: answer is (2,1,0) and (1,0,1)
(2,1,0) -> two '0' and one '010' -> ^0^010^ or ^010^0^ (111 can be placed in any one place marked as ^)
(1,0,1) -> one '0' and one '0110' ...
Condition 3: If 9 > Length > 6, you should consider the solution of two formulas:
Comments:
length – 3 : the length exclude 111
x: the times 0 occurred
y: the times 010 occurred
z: the times 0110 occurred
Finding all solutions {(x,y,z) | 1x + 3y + 4z = (Length - 3)} ----(1)
For each solution, you can generate one or more qualified string. For example, if you want to generate strings of length 10. One solution of (x,y,z) is (0,2,1), that means '010' should occurred twice and '0110' should occurred once. Based on this solution, the following strings can be generated:
0: x0 times
010: x 2 times
0110: x1 times
111: x1 times (must have)
Finding the permutations of elements above.
010-0110-010-111 or 111-010-010-0110 …
(Length - 6) = 1x + 3y + 4z ---(2)
Similar as above case, find all permutations to form an intermediate string.
Finally, for each intermediate string Istr, Istr + 011 or 110 + Istr are both qualified.
For example, (10-6) = 1*0 + 3*0 + 4*1 or = 1*1 + 3*1 + 4*0
The intermediate string can be composed by one '0110' for answer(0,0,1):
Then ^0110^011 and 110^0110^ are qualified strings (111 can be placed in any one place marked as ^)
Or the intermediate string can also be composed by one '0' and one '010' for answer (1,1,0)
The intermediate string can be 0 010 or 010 0
Then ^0010^011 and 110^0100^ are qualified strings (111 can be placed in any one place marked as ^)
Condition 4: If Length > 9, an addition formula should be consider:
(Length – 9) = 1x + 3y + 4z
Similar as above case, find all permutations to form an intermediate string.
Finally, for each intermediate string Istr, 110 + Istr + 011 is qualified.
Explaination:
The logic I use is based on Combinatorial Mathematics. A target string is viewed as a combination of one or more substrings. To fulfill the constraint ('111' appears exactly one time in target string), we should set criteria on substrings. '111' is definitely one substring, and it can only be used one time. Other substrings should prevent to violate the '111'-one-time constraint and also general enough to generate all possible target string.
Except the only-one-111, other substrings should not have more than two adjacent '1'. (Because if other substring have more than two adjacent 1, such as '111', '1111', '11111,' the substring will contain unnecessary '111'). Except the only-one-111, other substrings should not have more than two consecutive '1'. Because if other substring have more than two consecutive 1, such as '111', '1111', '11111,' the substring will contain unnecessary '111' . However, substrings '1' and '11' cannot ensure the only-one-111 constraint. For example, '1'+'11,' '11'+'11' or '1'+'1'+'1' all contain unnecessary '111'. To prevent unnecessary '111,' we should add '0' to stop more adjacent '1'. That results in three qualified substring '0', '010' and '0110'. Any combined string made from three qualified substring will contain zero times of '111'.
Above three qualified substring can be placeed anywhere in the target string, since they 100% ensure no additional '111' in target string.
If the substring's position is in the start or end, they can use only one '0' to prevent '111'.
In start:
10xxxxxxxxxxxxxxxxxxxxxxxxxxxx
110xxxxxxxxxxxxxxxxxxxxxxxxxxx
In end:
xxxxxxxxxxxxxxxxxxxxxxxxxxx011
xxxxxxxxxxxxxxxxxxxxxxxxxxxx01
These two cases can also ensure no additional '111'.
Based on logics mentioned above. We can generate any length of target string with exactly one '111'.
Your question could be clearer.
For one thing, does "1111" contain one occurrence of "111" or two?
If so, you want all strings that contain "111" but do not contain either "1111" or "111.*111". If not, omit the test for "1111".
If I understand you correctly, you're trying to construct an infinite subset of the infinite set of sequences of 0s and 1s. How you do that is probably going to depend on the language you're using (most languages don't have a way of representing infinite sets).
My best guess is that you want to generate a sequence of all sequences of 0s and 1s (which shouldn't be too hard) and select the ones that meet your criteria.