Generating Binary Encoded Symbols Python Program - python-3.x

I have an assignment that has instructions as follows:
write a program that reads in 4 sets of 4 dashed lines and outputs the four binary symbols that each set of four lines represents.
input consists of 16 lines in total, consisting of any number of dashes and spaces.
the first four lines represents a symbol, the next four lines represents the next symbol and so on.
print out the four binary-encoded symbols represented by the 16 lines in total.
each binary symbol should be on its own line
This is based upon a previous program that I wrote where input is a single line of text consisting of any number of spaces and dashes. If there is an even number of dashes in the line, output 0. Otherwise, output 1.
This is the code for the above:
line = input()
num_dashes = line.count("-")
mod = num_dashes % 2
if mod == 0:
print("0")
else:
print("1")
Please may someone assist me?
Thank you.

The code you have for processing one line is fine, although you could replace the if...else with just:
print(mod)
Now to extend this to multiple lines, it might be better not to call print like that, but to collect the output in a variable, and only output that variable when all 16 lines have been processed. This way the output does not get mixed with the input from the console.
So for instance, it could happen like this:
output = []
for part in range(4): # loop 4 times
digits = ""
for line in range(4): # loop 4 times
line = input()
num_dashes = line.count("-")
mod = num_dashes % 2
digits += str(mod) # collect the digit
output.append(digits) # append 4 digits to a list
print("\n".join(output)) # print the list, separated by linebreaks

Related

Problem when printing output in Python (with easyinput)

So I'm having trouble with enters and line breaks in my code. I must use easyinput library (and import read).
My code stands for:
Input: Input consists of several cases separated by an empty line. Every case has three parts ('lines'). The first one is a line with the translation table: 26 different characters (with no spaces nor ‘_’), the first one corresponding to ‘a’, the second one to ‘b’, …, and the last one to ‘z’. The second part is a number n > 0 in a line. The third part consists of n encrypted lines of text.
Output: For each case, write the original text, also with n lines. Change each ‘_’ of the encrypted text for a space. Write an empty line at the end of each case.
So I figured out how to solve the problem, the things is that my code prints 'well' entering line by line in input. But the problem input must be entered the whole entire. I put an example for better understanding:
Some input should be:
52-!813467/09*+.[();?`]<:>
6
5_3++!_305))_6*_;48_26)4+.)_4+);80_6*_;48_!8`60)_)85;
;]8*;:_+*8_!83(88)_5*!_;46(;88*_96*?;8)
*+(;485);_5*!_2:_*+(;4
956*_2(5*-4_)8`8*;4_0692_85);_)6!8
)4++;_1(+9_;48_081;_8:8_+1_;48_!85;4)_485!
5_288_06*8_1(+9_;48_;(88_;4(+?34_;48_)4+;_161;:_188;_+?;
bcdefghijklmnopqrstuvwxyza
3
cfxbsf_pg_cvht_jo_uif_bcpwf_dpef
j_ibwf_pomz_qspwfe_ju_dpssfdu
opu_usjfe_ju
And its output must be:
a good glass in the bishops hostel in the devils seat
twenty one degrees and thirteen minutes
northeast and by north
main branch seventh limb east side
shoot from the left eye of the deaths head
a bee line from the tree through the shot fifty feet out
beware of bugs in the above code
i have only proved it correct
not tried it
My code far now is:
from easyinput import read
abc = 'abcdefghijklmnopqrstuvwxyz'
values = [letter for letter in abc]
old_abc = read(str)
while old_abc is not None:
keys = [old_letter for old_letter in old_abc]
dict_abc = dict(zip(keys, values))
num_lines = read(int)
for i in range(num_lines):
line = read(str)
for j in line:
if j == '_':
print(' ', end = '')
else:
print(dict_abc[str(j)], end = '')
print('\n')
old_abc = read(str)
I do not find a way of making my code easier, I just want some help to finally print the desired output. Thanks

Reads a series of lines Python

Can someone enlighten me how to do this?
Write a Python program that reads a series of lines one by one from the keyboard (ending by an empty line) and, at the end, outputs the number of times that the first line occurred. For example, if it reads
hello
world
We say hello
hello
Birkbeck
hello
it would output 3 since the first line ("hello") occurred three times.
You may assume that the user enters at least two non-empty lines before the empty line.
not sure if you want to separate word with spaces or with the newline character and if we count the first occurence.
This is a sample solution for spaces separation between words. This works for the example that you provided
freq_dict = {}
word = input().split()
for w in word:
if w not in freq_dict.keys():
freq_dict[w] = 0
else:
freq_dict[w] += 1
print(freq_dict[word[0]])

Difference resulting from position of print in loops

First question here.
I am picking up Python and have a question that may be quite basic.
I am trying to create this pattern with a nested loop:
x
x
x
xxx
With the code:
numbers = [1,1,1,3]
for count_x in numbers:
output = ""
for count in range(count_x):
output +=x
print(output)
My question is - why does my output change when I move the position of print(output).
The code above works but when I align print(output) with the for count_x in numbers:, I only get "xxx", when I align print(output) to output +=x, I get the following:
x
x
x
x
xx
xxx
which is very odd because there are only 4 items in the list and it shows me 6 lines of output.
Could someone please help? Really puzzled. Thank yall very much.
There's a difference between these two bits of code (fixing the x/"x" problem along the way - your code won't actually work as is unless you have a variable x):
# First:
numbers = [1,1,1,3]
for count_x in numbers:
output = ""
for count in range(count_x):
output += "x"
print(output)
# Second:
numbers = [1,1,1,3]
for count_x in numbers:
output = ""
for count in range(count_x):
output += "x"
print(output)
In the second, the print is done inside the loop that creates the string, meaning you print it out multiple times while building it. That's where your final three lines come from: *, ** and ***. This doesn't matter for all the previous lines since there's no functional difference. Printing a one character string at the end or after adding each of the one characters has the same effect.
In the first, you only print the string after fully constructing it.
You can see this effect by using a slightly modified version that outputs different things for each outer loop:
numbers = [1,1,1,3]
x = 1
for count_x in numbers:
output = ""
for count in range(count_x):
output += str(x)
print(output)
x += 1
This shows that the final three lines are part of a single string construction (comments added):
1 \
2 >- | Each is one outer/inner loop.
3 /
4 \ | A single outer loop, printing
44 >- | string under construction for
444 / | each inner loop.
In any case, there are better ways to do what you want, such as:
numbers = [1,1,1,3]
for count_x in numbers:
print('x' * count_x)
You could probably also do it on one line with a list comprehension but that's probably overkill.

Set in python replacing number 10 from a file with 0,1

I have a file with the line:1 2 3 4 5 10. When I add this line to a set in Python, I get {1,2,3,4,5,0} instead of {1,2,3,4,5,10}. How do I code so that I get the 10 inside the set instead of it recognizing it as a 1 and a 0?
EDIT: This was the code I wrote:
states = set()
line = open("filepath", "r").readlines()[0]
states.add(line)
print (states)
Input file content:
1 2 3 4 5 10
As set cannot have a same number twice, the zero which belongs to 10 is being treated as a unique element thus set cannot contain two same elements.
Do something like this to fix it (Assuming you don't have newline characters, if you do, just use the strip method.):
line = open("filepath", "r").readlines()[0]
line = line.split(' ') #Split by Space
number_set = set(line) #Since file is a list after splitting.

Fortran Program of Strings: Counting commas

I am doing this assignment for my class and I don't know if I am doing it right.
The assignment is to design and implement a Fortran program to read a series of strings from the keyboard and count the commas (“,”) in each line.
The program should provide loops, formatted input/output, and processing strings.
The program should output the line number, the number of commas, and the first 70 characters of the input line.
Input will be terminated with a blank line or 9999 lines, whichever comes first.
Test the program on a series of different input values and verify that the output is correct for those input values. Assume the input string may be no longer than 999 characters.
Output shall be a formatted list of the format:
nnnn:cc:aaaaaaaaaaaa…aaaaaaaaaaaaaaaa where nnnn is the line number, cc is the comma count, and aaaaaaaaaaaa…aaaaaaaaaaaaaaaa is the first 70 characters of the input line without trailing spaces.
This code for this program has to be simple enough for a beginner programmer to understand. Nothing complex.
Here is what I have so far:
Program counting
implicit none
Integer :: line_number
Integer :: comma_count
Character (len = 999) :: message
WRITE (*,*)
WRITE (*,*)"Provide output the line number, the number of commas,&
and the first 70 characters of the input line."
WRITE (*,*)
DO line_number = 0, 9999
line_number = 1 + line_number
IF (line_number == 0 .or. line_number >= 9999) then
Exit
END IF
WRITE (*,*)":"
comma_count = 1 + line_number
WRITE (*,*)":"
WRITE (*,'(a70)') message
END DO
READ (*,*) line_number, comma_count, message
END Program counting

Resources