Caesar cipher with multiple values - python

I'm new to stack overflow.
I'm trying to create a Caesar cipher that can take multiple inputs for shifts. I have all my shifts stored in a list. The message the user is attempting to encrypt is also stored in a list like so:
["Hello this is what the message looks like.", "It is stored in a list like so.", "It is pretty neet."]
The shift amount must start over on the next "line". So if the last e in like is shifted by 4 in a shift sequence of
[1,2,3,4,5],
the I in It would be shifted by 1. I tried using a for loop to to search for each letter then use ord() to obtain the UTF code then add the shift but that did not work.I suspect this is because my shift values are held in a list. How would I approach this problem?
***EDIT***I am reposing this topic because my previous topic was closed for being "duplicate". However, I don't think it is a duplicate topic. The linked topic involved a shift amount that will always be constant. My program requires a shift amount that various depending on what the user inputs. My program also has a list containing the shift values. I cannot use some operations because it is a list.

Related

How to calculate the marks for each group and deduce the average score under each group,

UPDATED QUESTION AND CODE!30.01.2021
This question logically came up after this question - How to find a separate attribute among the records and form a separate collection on it?
I need to calculate the average score of each group.
In the code that the user #itprorh66 suggested, I added a formula for calculating the average score(it works) and a conditional search operator for the grade.
Mister #itprorh66
I decided to modify your code a bit to try to solve my problem myself. But so far unsuccessful.
This code block is working
def srtgr(self):
counthrees=0
countfours=0
countfives=0
midl_average=0
gl=dict() #Dictionary for the group
for s in studinfos:
sd=gl.pop(s.numbgroup,dict())
sd[s.surn]=[s.markgeometry,s.markalgebra,s.markinformatika]
gl[s.numbgroup]=sd
for k,v, in gl.items():
s=f'Group: {k}: \n'
for stdn,grds in v.items():
s+=f' {stdn}\t{grds[0]},{grds[1]},{grds[2]} \n'
print(s)
This is the second part in def srtgr. In this code block I trying to calculate the number of grades (3,4 and 5) in each group, and then find the average grade for each group. With the help of a loop, I start traversing the dictionary and with the help of the branch operator I start counting the scores in each group, but that's what I thought when I wrote this code. In reality, nothing starts and does not count
for grds in gl.items():
if grds.markgeometry==3:
counthrees+=1
if grds.markalgebra==3:
counthrees+=1
if grds.markinformatika==3:
counthrees+=1
if grds.markgeometry==4:
countfours+=1
if grds.markalgebra==4:
countfours+=1
if grds.markinformatika==4:
countfours+=1
if grds.markgeometry==5:
countfives+=1
if grds.markalgebra==5:
countfives+=1
if grds.markinformatika==5:
countfives+=1
midl_average = (counthrees+countfours+countfives)/3
for kk,grds1 in gl.items():
grds=f'Group: {kk}: {grds1}\n'
print(grds)
print(*sorted(gl, key = midl_average, reverse = True))
INPUT DATA
My reputation is litlle and I can not add the picture form my computer and I send the link,where my input data, what I add in class and how my object classes collection showing in the console. And the separated of groups
https://ibb.co/zV1B0HC
How to make it so that the average grade is displayed at the bottom after the list of students in each group?
Or just calculate the average score in each group and enter it into another dictionary?
I figured out the sorting of values ​​in the dictionary.
I tried to make a 2nd dictionary and put the groups there, and then calculate it in a separate assessment unit - it didn't work.
Then I read that in Python you can add a dictionary to a dictionary.
As a result, I tried to add the existing dictionary to the new one, but did not understand how to proceed.
Because grades must be calculated anyway
You cannot use any helper classes, work with only one class.
dict.items() returns key value pairs in a dictionary. Based on the code the v dictionary has two kinds of data
key is a student number and value is a list of grades.
key is a student numbgroup and the value is a list with an empty list in it.
My best guess is you don't want the second type of thing in that dictionary. I can't tell what the redball stuff is supposed to do but currently you make it an empty list, put it in a dictionary that is carrying a totally different kind of info and then overwrite the variable with a float without ever messing with the earlier empty list.
As an aside, don't be afraid of making verbose variable names. It's harder to follow the code when we need to work out what k and v are. That we includes you by the way, even with your own code bad naming can make it harder to diagnose errors.

Using Python & NLP, how can I extract certain text strings & corresponding numbers preceding the strings from Excel column having a lot of free text?

I am relatively new to Python and very new to NLP (and nltk) and I have searched the net for guidance but not finding a complete solution. Unfortunately the sparse code I have been playing with is on another network, but I am including an example spreadsheet. I would like to get suggested steps in plain English (more detailed than I have below) so I could first try to script it myself in Python 3. Unless it would simply be easier for you to just help with the scripting... in which case, thank you.
Problem: A few columns of an otherwise robust spreadsheet are very unstructured with anywhere from 500-5000 English characters that tell a story. I need to essentially make it a bit more structured by pulling out the quantifiable data. I need to:
1) Search for a string in the user supplied unstructured free text column (The user inputs the column header) (I think I am doing this right)
2) Make that string a NEW column header in Excel (I think I am doing this right)
3) Grab the number before the string (This is where I am getting stuck. And as you will see in the sheet, sometimes there is no space between the number and text and of course, sometimes there are misspellings)
4) Put that number in the NEW column on the same row (Have not gotten to this step yet)
I will have to do this repeatedly for multiple keywords but I can figure that part out, I believe, with a loop or something. Thank you very much for your time and expertise...
If I'm understanding this correctly, first we need to obtain the numbers from the string of text.
cell_val = sheet1wb1.cell(row=rowNum,column=4).value
This will create a list containing every number in the string
new_ = [int(s) for s in cell_val.split() if s.isdigit()]
print(new_)
You can use the list to assign the values to the column.
Then define the value of the 1st number in the list to the 5th column
sheet1wb1.cell(row=rowNum, column=5).value = str(new_[1])
I think I have found what I am looking for. https://community.esri.com/thread/86096 has 3 or 4 scripts that seem to do the trick. Thank you..!

Contig Extension with Python

I want to add a function to a program that creates dictionaries with dna sequences that receives a contig (incon= initial contig; dna sequence) and extends it to the right by finding overlapping parts in form of keys in dictionaries and concatenating the values with the "+" operator.
I'll give a quick example:
GATTTGAAGC as initial contig
ATTTGAAGC:A is one of many entries in the dictionary
I want the function to search for such an overlapping part (I asked this here yesterday and it worked fine by itself and with specific values but not within the function with variables) that is a key in the dictionary and concatenate the value of that key to the initial sequence (extend the contig to the right) and save the new sequence into incon then delete this dictionary-entry and repeat until there are no entries left (this part i haven't even tried yet).
First i want the function to search for keys with length of 9 with values of length 1 (ATTTGAAGC:A) and if there are no overlapping parts for keys with length 8 with values of length 2 (f.e. ATTTGAAG:TG) and so on.
Additional Info:
The Dictionary "suffixDicts" has such entries with values with length from 1 (key has length 14) to 10 (key has length 5).
"reads" is where a list of sequences is stored
When i try to do the steps one after another some work (like the search) and some don't but when i tried to built a function out of it, literally nothing happens. The function is supposed to return the smallest possible extension.
def extendContig (incon, reads, suffixDicts):
incon = reads[0]
for x in range(1,len(incon)):
for key in suffixDicts.keys():
if incon[x:] == key:
incon = incon+suffixDicts['key']
print(incon)
else:
print("n")
return()
I'm very new to Python and there probably are very dire mistakes i made and i would like them to be pointed out. I know that I'm way over my head with this but I'm understanding most parts of the already existing code now but still have problems with implementing something by myself into it, probably due to incorrect synthax. I know there are programs i could use but i would like to understand the whole thing behind it.
edit: As asked for i will add the already given functions. Some of them were already written some parts i wrote based on the given code (basically i copied it with some tweaks). Warning: It is quite a lot:
Reading the Fasta file:
Additional Info:
The Fasta file contains large amounts of sequences in the Form:
"> read 1
TTATGAATATTACGCAATGGACGTCCAAGGTACAGCGTATTTGTACGCTA
"> read 2
AACTGCTATCTTTCTTGTCCACTCGAAAATCCATAACGTAGCCCATAACG
"> read 3
TCAGTTATCCTATATACTGGATCCCGACTTTAATCGGCGTCGGAATTACT
I uploaded the file here: http://s000.tinyupload.com/?file_id=52090273537190816031
edit: edited the large blocks of code out it doesn't seem to be necessary.

Speed/structure optimization for a recursive tree

Edit -> short version:
In Python, unlike in C, if I pass a parameter to a function I -say: a dict-, the changes made within the function call will reflect outside (as if I passed a pointer instead of just the value)
I want to avoid this so:
-> I make a copy of my dict and pass the copy to my function
But the values of my dict can be some dict and this goes on until an undefinite depth
-> the recursive copy is very long.
Question: what is a pythonic way to go about this?
Long version:
I'm coding a master-mind playing robot with a n-digit code in Python.
You try to guess the code and for each try you get an answer in terms of how many white/black/none you have, meaning resp. "good digit good position"/"good digit wrong position"/"wrong digit" (but you don't know to which digit the whites/blacks/none refer)
I analyze the answers and build a tree of possibilities with a dictionary storing white/black/none.
I store a map of the possible positions of the numbers 0-9 within the code (a digit can appear more than once) in a list.
Ex: for a 3-digit game I will have [[x,y1,y2,y3][-1,0,1,4][...][...][][][][][][]] with:
x: the total number of times this digit appears in the code (default value being n+1, ie. 4 in the exemple) with positive meaning sure and negative "at least"
y1,y2,..,yn the position within the code: 1 means I know the digit is in this position, 0 I know it's not, and 4 (or anything) as default
In my exemple: I know that '1' appears at least once in the code (-1) that it is present in position 2 and that it is NOT present in position 1 and that position 3 is still hypothetically possible.
While I explore my tree of possibilities, I update this list. Which means that each branch of the tree will have its own copy of the list.
Since I recently discovered that, unlike in C, when I pass my list to a sub-method, any change made to it within the sub will reflect on the list outside, I manually copy my list each time with a small method:
def bak_symb(_s):
_b = [[z for z in _s[i]] for i in xrange(10)]
return _b
Now, I profiled my programm and noticed that 90% of the time is spent either in
append()
(the branches of my tree are nested dictionaries {w:{},b:0,n:{}} to which I append each branch of possibilities that I explore)For each branch : the programm has to find a n-digit code
or
my copying function
So I have three questions.
Is there a way to make this function faster?
Is there a something better adapted than the structures I chose (2-depth list for the symbols and nested dict for the hypothesis)
Is there a more adequate way of doing this than building this huge tree
All comments and remarks are welcome.
I'm self-taught in and might have missed some obvious pythonic way of doing some things.
Last but not least, I tried to find a good compromise between making this short and clear, here again don't hesitate to ask for more details.
Thanks in advance,
Matt

Python subset Sum with floats?

I'm trying to see if a float can be constructed by adding values from a list of floats. Since the values are experimental, there is another array that contains error room values, such that the float can be constructed from the float list plus 0 or 1 of every value in the error room list, plus some additional error margin. The float may or may not be constructed from these parameters.
The length of lists, error margin and maximum size of the combination values are user defined.
I thought an efficient way to solve this problem was to have a function create and store every possible combination using the given parameters in an array, then check each float to see if it matches the combination, then print out the way the combination was obtained if a match exists.
for example, given a numlist [132.0423,162.0528,176.0321](3-10 values total)
and an errorlist [2.01454,18.0105546] (0-4 values total)
and an error room of 2, and a maximum combination size of 1000,
the float 153.0755519 can be constructed by 132.0423+2.01454+18.0105546 or numlist[0]+errorlist[0]+errorlist[1] and be within the error room
I have no idea how to go about solving such a problem. Perhaps using dynamic programming? I was thinking it would be computationally efficient to create the combinations array once via a separate function, then continuously pass it into a comparison function.
The background: the large floats are fragment masses that come from a mass spectrometer output and my team is attempting to analyze which fragments came from our initial protein that can only fragment into the pieces defined in numlist, but can occasionally lose a small functional group (water, alcohol, hydrogen etc.)

Categories