Ciphering through the non letters - python

So here's my problem...I’m trying to make it so any character not part of the alphabet isn’t shifted in the text file but it does 'take up' a shift amount. For example, if I wanted to implement 2 shift amounts - 3 shifts and 4 shifts - to the statement "alarm clock" the a would be shifted by 3, the l by 4, the second a by 3, r by 4, m by 3, and then the space wouldnt be shifted but would "take up" a space, so it would cause the c to shift by 3. Here's what I have so far
import sys
file = input("Enter input file: ")
shifts = input("Enter shift amounts: ").split()
code = input("Encode (E) or Decode (D)?")
if code == "E":
with open(file) as f:
lines = f.read().replace("e","zw").splitlines()
for line in lines:
mid = len(line) // 2
line_list = list(line)
line_list.insert(mid,'hokie')
new_line = ('hokie' + (''.join(line_list) + 'hokie'))
for index, char in enumerate(new_line):
index = index % len(shifts)
print(chr(ord(char)+int(shifts[index])),end='')
print()
example input:
file text:
Though Birnam Wood be come to Dunsinane,
And thou opposed, being of no woman born,
Yet I will try the last. Before my body
I throw my warlike shield. Lay on, Macduff;
And damned be him that first cries "Hold, enough!"
-- MacBeth
shift:
3 4 5 6 7
result:
kspolWltank Goyqer Drsi icltqpha ivpdb ar Iauvmsguca,nvnmj
kspolDri aksz vsttygzh, icltqphantn sk ur butdr hvur,nvnmj
kspolBdbz L boso yxf xmfd pmurlifya. Hgzjtxgz re esieoronk
kspolL ynyra sf afxsloec vlnnvnmjfdoh. Oed vq, Shfhzlm;ltqph
kspolDri kdqsfdg gfd lns wlfz immurliwya gwogzw "Orpi, casubjl!"oronk
kspol-- ShkspolfFecakltqph

you have to add a if,here my solution.for the punctuation,
please lookBest way to strip punctuation from a string in Python
import string
exclude = set(string.punctuation)
shifts = [3,4]
lines = ["alarm clock", "alarm ';.clock"]
for line in lines:
mid = len(line) // 2
line_list = list(line)
line_list.insert(mid,'hokie')
new_line = ('hit' + (''.join(line_list) + 'hit'))
index = 0
for char in new_line:
index = index % len(shifts)
if(char in exclude):
print(char,end='')
continue
print(chr(ord(char)+int(shifts[index])),end='')
index += 1

Related

I am struggling a little on getting this code to work in python

Now honestly, I think this could be entirely wrong as I don't really know what I am doing and just kinda through some stuff together, so help would be appreciated.
This is the code I got, including starting code that cannot be changed.
# DO NOT CHANGE ANY CODE IN THE MAIN FUNCTION
def main():
input_file = open('strings.txt', 'r') # Open a file for reading
for line in input_file: # Use a for loop to read each line in the file
manipulate_text(line)
print()
def manipulate_text(line):
# Delete the following line, then implement the function as indicated
line = line.upper()
line = line.strip()
letters = []
for char in line:
if char.isalpha():
if char not in letters.count(line):
letters[char] = 1
else:
letters[char] += 1
for everyLetter in letters:
print("{0} {1}".format(everyLetter, letters[everyLetter]))
The .txt file it uses just contain:
Csc.565
Magee, Mississippi
A stitch in time saves nine.
And these are the instructions I have been given, also in this .count is what needs to be used, as shown in my code.
The manipulate_text() function accepts one string as input. The function should do the following with the string parameter:
⦁ Convert all the letters of the string to uppercase, strip the leading and trailing whitespace, and output the string.
⦁ Count and display the frequency of each letter in the string. Ignore all non-alpha characters.
For example, if this is the contents of strings.txt:
Csc.565
Magee, Mississippi
A stitch in time saves nine.
This would be the output of your program:
CSC.565
C 2
S 1
MAGEE, MISSISSIPPI
M 2
A 1
G 1
E 2
I 4
S 4
P 2
A STITCH IN TIME SAVES NINE.
A 2
S 3
T 3
I 4
C 1
H 1
N 3
M 1
E 3
V 1
Here's the code you wanted:
# DO NOT CHANGE ANY CODE IN THE MAIN FUNCTION
def main():
input_file = open('strings.txt', 'r') # Open a file for reading
for line in input_file: # Use a for loop to read each line in the file
manipulate_text(line)
print()
def manipulate_text(line):
line = line.upper()
line = line.strip()
letters = {} # Dict[Char: No. of occurrences]
print(line)
for char in line:
if char.isalpha():
if char not in list(letters.keys()): # If char not in our dict
letters[char] = 1 # One occurrence
else:
letters[char] += 1 # Add one occurrence
for i in letters:
print("{0} {1}".format(i, letters[i]))
main() # Call main
Output:
CSC.565
C 2
S 1
MAGEE, MISSISSIPPI
M 2
A 1
G 1
E 2
I 4
S 4
P 2
A STITCH IN TIME SAVES NINE.
A 2
S 3
T 3
I 4
C 1
H 1
N 3
M 1
E 3
V 1
In response to your comment:
# DO NOT CHANGE ANY CODE IN THE MAIN FUNCTION
def main():
input_file = open('strings.txt', 'r') # Open a file for reading
for line in input_file: # Use a for loop to read each line in the file
manipulate_text(line)
print()
def manipulate_text(line):
line = line.upper()
line = line.strip()
letters = {} # Dict[Char: No. of occurrences]
print(line)
for char in line:
if char.isalpha():
if list(letters.keys()).count(char) == 0: # If char not in our dict
letters[char] = 1 # One occurrence
else:
letters[char] += 1 # Add one occurrence
for i in letters:
print("{0} {1}".format(i, letters[i]))
main() # Call main
In reponse to your second comment:
Use these instead of manipulate_text():
If you don't care about ordering:
def manipulate_text(line):
line = [i for i in line.upper() if i.isalpha()] # List comprehension!
for i in set(line): # set() changes it to all unique keys, loses order
print(i, line.count(i)) # .count()
If you care about ordering:
def manipulate_text(line):
line = [i for i in line.upper() if i.isalpha()] # List comprehension!
uniques = []
for i in line:
if i not in uniques:
print(i, line.count(i)) # .count()
uniques += [i]

Sourcing and compressing txt file, then compressing content to a maximium length value of 50

Introduction and Explaination
I want to take a two functions (filename and maximum length), where the function is opened, reads all lines, and return strings where the strings defines a line that is filled without exceeding a maximum length defined as a variable (in this case, lineMax = 50 characters)
So the aim is for this is as follows:
"['Alice was beginning to get very tired of sitting',
'by her sister on the bank, and of having nothing',
'to do: once or twice she had peeped into the book',
'her sister was reading, but it had no pictures or',
'conversations in it, "and what is the use of a',
'book," thought Alice, "without pictures or',
'conversations?"']"
The result is that anything can go in as long there is a maximum of 50 characters. the rules defined is that you cannot group together words from different paragraphs, and that no words in the txt file is longer than the maximum length.
What I have tried
In thinking about this, I've formulated this psuedocode to see if this would be viable:
def consistentLineLength(*file_name):
# Opening, reading and writing lines from file.
file_name = open('words.txt', 'w')
lines = file_name.readlines()
file_name.writelines(file_name)
lineMax = 50
file_name = open('words.txt', 'r')
text1 = []
text2 = []
text3 = []
text4 = []
text5 = []
text6 = []
text7 = [] # Empty lists/containers for values.
for line in fileread:
splitLine = line.split(",")
text1.append(splitLine[0]) #
text2.append(splitLine[1].strip()) # Result: ['SAHFS DGDGBD etc'], all compressed up to a value of 50.
text3.append(splitLine[2].strip())
text4.append(splitLine[3].strip())
text5.append(splitLine[4].strip())
text6.append(splitLine[5].strip())
text7.append(splitLine[6].strip()) # .strip() removes backslash \n from ends.
print(line)
lengthlist1 = 0
for length1 in text1:
if length1 >= 0 and length1 < lineMax: # Needs to be a positive integer number, like this. Should be the max number of characters in a string to fill.
lengthlist1 += 1
print (length1)
lengthlist2 = 0
for length2 in text2:
if length2 >= 0 and length2 < lineMax: # Greater than 0, but less than 50.
lengthlist2 += 1
print (length2)
lengthlist3 = 0
for length3 in text3:
if length3 >= 0 and length3 < lineMax:
lengthlist3 += 1
print (length3)
lengthlist4 = 0
for length4 in text4:
if length4 >= 0 and length4 < lineMax:
lengthlist4 += 1
print (length4)
lengthlist5 = 0
for length5 in text5:
if length5 >= 0 and length5 < lineMax:
lengthlist5 += 1
print (length5)
lengthlist6 = 0
for length6 in text6:
if length6 >= 0 and length6 < lineMax:
lengthlist6 += 1
print (length6)
lengthlist7 = 0
for length7 in text7:
if length7 >= 0 and length7 < lineMax:
lengthlist7 += 1
print (length7)
file_name.close() # Close file.
So can be seen, this is a for loop solution with separate sentence lengths defined separately. Is there an algorithm which can make this process more efficient and workable for use?
you may use wrap:
from textwrap import wrap
text = "1234567890123"
texts = wrap(text, 5)
print(texts)
prints
['12345', '67890', '123']

Program that reads a vote text file and prints the outcome not printing the right numbers

Here is my code I need it to read a line of text that just composes of y's a's and n's y meaning yes n meaning no a meaning abstain, I'm trying to add up the number of yes votes. The text file looks like this:
Aberdeenshire
yyynnnnynynyannnynynanynaanyna
Midlothian
nnnnynyynyanyaanynyanynnnanyna
Berwickshire
nnnnnnnnnnnnnnnnnnnnynnnnnynnnnny
here is my code:
def main():
file = open("votes.txt")
lines = file.readlines()
votes = 0
count = 0
count_all = 0
for m in range(1,len(lines),2):
line = lines[m]
for v in line:
if v == 'a':
votes += 1
elif v == 'y':
count_all += 1
count += 1
votes += 1
else:
count_all += 1
print("percentage:" + (str(count/count_all)))
print("Overall there were ", (count/count_all)," yes votes")
main()
First of all, you should note that your file.readlines() actually gives you the \n at the end of each line, which in your code will both be treated in the else block, so as no's:
>>> with open("votes.txt","r") as f:
... print(f.readlines())
...
['Aberdeenshire\n',
'yyynnnnynynyannnynynanynaanyna\n',
'Midlothian\n',
'nnnnynyynyanyaanynyanynnnanyna\n',
'Berwickshire\n',
'nnnnnnnnnnnnnnnnnnnnynnnnnynnnnny\n']
So that might explain why you don't find the good numbers...
Now, to make the code a bit more efficient, we could look into the count method of str, and maybe also get rid of those \n with a split rather than a readlines:
with open("votes.txt","r") as f:
full = f.read()
lines = full.split("\n")
votes = 0
a = 0
y = 0
n = 0
for m in range(1,len(lines),2):
line = lines[m]
votes += len(line) # I'm counting n's as well here
a += line.count("a")
y += line.count("y")
n += line.count("n")
print("Overall, there were " + str(100 * y / (y + n)) + "% yes votes.")
Hope that helped!
More or less pythonic one liner, it doesn't give you the votes for each person/city tho:
from collections import Counter
l = """Aberdeenshire
yyynnnnynynyannnynynanynaanyna
Midlothian
nnnnynyynyanyaanynyanynnnanyna
Berwickshire
nnnnnnnnnnnnnnnnnnnnynnnnnynnnnny"""
Counter([char for line in l.split('\n')[1::2] for char in line.strip()])
Returns:
Counter({'a': 11, 'n': 60, 'y': 22})

Print data between positions within a loop

I have one files.
File1 which has 3 columns. Data are tab separated
File1:
2 4 Apple
6 7 Samsung
Let's say if I run a loop of 10 iteration. If the iteration has value between column 1 and column 2 of File1, then print the corresponding 3rd column from File1, else print "0".
The columns may or may not be sorted, but 2nd column is always greater than 1st. Range of values in the two columns do not overlap between lines.
The output Result should look like this.
Result:
0
Apple
Apple
Apple
0
Samsung
Samsung
0
0
0
My program in python is here:
chr5_1 = [[]]
for line in file:
line = line.rstrip()
line = line.split("\t")
chr5_1.append([line[0],line[1],line[2]])
# Here I store all position information in chr5_1 list in list
chr5_1.pop(0)
for i in range (1,10):
for listo in chr5_1:
L1 = " ".join(str(x) for x in listo[:1])
L2 = " ".join(str(x) for x in listo[1:2])
L3 = " ".join(str(x) for x in listo[2:3])
if int(L1) <= i and int(L2) >= i:
print(L3)
break
else:
print ("0")
break
I am confused with loop iteration and it break point.
Try this:
chr5_1 = dict()
for line in file:
line = line.rstrip()
_from, _to, value = line.split("\t")
for i in range(int(_from), int(_to) + 1):
chr5_1[i] = value
for i in range (1, 10):
print chr5_1.get(i, "0")
I think this is a job for else:
position_information = []
with open('file1', 'rb') as f:
for line in f:
position_information.append(line.strip().split('\t'))
for i in range(1, 11):
for start, through, value in position_information:
if i >= int(start) and i <= int(through):
print value
# No need to continue searching for something to print on this line
break
else:
# We never found anything to print on this line, so print 0 instead
print 0
This gives the result you're looking for:
0
Apple
Apple
Apple
0
Samsung
Samsung
0
0
0
Setup:
import io
s = '''2 4 Apple
6 7 Samsung'''
# Python 2.x
f = io.BytesIO(s)
# Python 3.x
#f = io.StringIO(s)
If the lines of the file are not sorted by the first column:
import csv, operator
reader = csv.reader(f, delimiter = ' ', skipinitialspace = True)
f = list(reader)
f.sort(key = operator.itemgetter(0))
Read each line; do some math to figure out what to print and how many of them to print; print stuff; iterate
def print_stuff(thing, n):
while n > 0:
print(thing)
n -= 1
limit = 10
prev_end = 1
for line in f:
# if iterating over a file, separate the columns
begin, end, text = line.strip().split()
# if iterating over the sorted list of lines
#begin, end, text = line
begin, end = map(int, (begin, end))
# don't exceed the limit
begin = begin if begin < limit else limit
# how many zeros?
gap = begin - prev_end
print_stuff('0', gap)
if begin == limit:
break
# don't exceed the limit
end = end if end < limit else limit
# how many words?
span = (end - begin) + 1
print_stuff(text, span)
if end == limit:
break
prev_end = end
# any more zeros?
gap = limit - prev_end
print_stuff('0', gap)

Finding Multiple Sequences in a File by Position after SpecifIcally Repeating Characters

i am trying to write this code, so that i can get my sequences of different samples in a file after line breaks by position, the output is always blank for some reason, can you help me?
import readline
count = 0
brk = 0
with open("file.txt") as f:
while (count < 35):
l = f.readline()[brk + 2]
sp = raw_input ("Starting Position:")
sp = int(sp)
rl = sp + 6
print(l[sp:rl])
print(l[-30:0])
count = count + 1
brk = brk + 2
print ("Done")
In the line l = f.readline()[brk + 2] the program puts one character into variable l. So, when you are trying to print substring of l (in the lines print(l[sp:rl]) and print(l[-30:0])), the program prints empty lines. It is expected result.
To find this you could just add print l right after assigning of l.
It seems that you are trying to read 2-nd, 4-th, 6-th, etc lines of the file. To do it you can do something like this:
brk = 0
with open("file.txt") as f:
f.readline()
f.readline() #skip both first lines
while (count < 35):
l = f.readline()
f.readline() #skip next line
sp = raw_input ("Starting Position:")
sp = int(sp)
rl = sp + 6
print(l[sp:rl])
print(l[-30:0])
count = count + 1
brk = brk + 2
Also print(l[-30:0]) must always print empty line. It seems that you need print(l[-30:]) (last 30 characters of the string l).

Categories

Resources