# Demo entry 6783085

total

Submitted by anonymous on Jan 28, 2019 at 14:58
Language: Python 3. Code size: 7.4 kB.

```###1ο Εργαστήριο

t = input("Please enter some text: ")
l = t.split()
print(max(l))

t = input("Please enter some text: ")
l = t.split()
l.sort(reverse=True)
print(l[0]+" "+l[1])

###2ο Εργαστήριο

from string import ascii_letters
d = {}
t = input("Please enter some text: ")
for c in t:
if c in ascii_letters:
c = c.upper()
if c in d:
d[c]+=1
else:
d[c]=1
for c in d:
print(c+':',d[c])

from string import ascii_letters
d = {}
t = input("Please enter some text: ")
for c in t:
if c in ascii_letters:
c = c.upper()
if c in d:
d[c]+=1
else:
d[c]=1
l = list(d.items())
l.sort()
for x in l:
print(x[0]+":", x[1])

###3ο Εργαστήριο

# This program takes as input a positive integer and prints all prime numbers
# up to that integer
# The program is fast because it checks primality by comparing the currently
# tested number only with the primes found so far
n = int(input('Please enter a natural number greater than 1: '))
primes = []
for tested in range(2,n+1):
for p in primes:
if not tested % p: # exact division by p
break
else:
primes.append(tested)
print(primes)

i = 0
factors = []
div = n
while div>1:
if not div%primes[i]:
factors.append(primes[i])
div = div//primes[i]
else:
i+=1
print(n,'= ',end='')
for i in range(len(factors)-1):
print(str(factors[i])+'*',end='')
print(factors[-1])

###4ο Εργαστήριο

def encode(text, key):
'returns encoded text'
enc = ''
for c in text:
if c != ' ':
normord = ord(c)-ord('a') # normal order of char between 0 and 25
newnormord = (normord + key) % 26 # normal order of encoded char
neword = newnormord+ord('a') # ASCII order of encoded char
enc += chr(neword) # encoded char
else:
enc += c
return enc

def decode(text, key):
'returns decoded text'
dec = ''
for c in text:
if c != ' ':
dec += chr((ord(c)-ord('a')-key)%26 + ord('a'))
else:
dec += c
return dec

def breakcode(plaintext, cyphertext):
if len(plaintext)==0 or len(plaintext)!=len(cyphertext):
return -1
for key in range(26):
if encode(plaintext,key)==cyphertext:
return key
else:
return -1

def breakcode2(plaintext, cyphertext):
if len(plaintext)==0 or len(plaintext)!=len(cyphertext):
return -1
pos = 0
while pos<len(plaintext) and plaintext[pos]==' ':
if cyphertext[pos] != ' ':
return -1
pos += 1
if pos==len(plaintext):
return -1

# tentative key is the first shift
key = (ord(cyphertext[pos])-ord(plaintext[pos])) % 26
pos += 1
while pos<len(plaintext):
if cyphertext[pos]==' ':
if plaintext[pos]!=' ':
return -1
else:
pos+=1
continue

key2 = (ord(cyphertext[pos])-ord(plaintext[pos])) % 26
if key != key2:
return -1
pos += 1
return key

s = input("Input the text for encoding: ")
key = -1
while key<1 or key>25:
key = int(input("Input a key between 1 and 25: "))
s2=encode(s,key)
print(s2)
print(decode(s2,key))
print(breakcode2('hello world', 'lipps asvph'))
print(breakcode2('hello world', 'lipps asbph'))

###5ο Εργαστήριο

#non-recursive version
def decimal_to_roman(n):
mapping = [['','I','II','III','IV','V','VI','VII','VIII','IX'],
['','X','XX','XXX','XL','L','LX','LXX','LXXX','XC'],
['','C','CC','CCC','CD','D','DC','DCC','DCCC','CM'],
['','M','MM','MMM']]
if n<1 or n>3999:
return ''
res=[]
i=0
while(n):
y = n % 10
res.append(mapping[i][y])
n = n//10
i+=1
res.reverse()
outp = ''.join(res)
return outp
#recursive version
def decimal_to_roman2(n,level):
mapping = [['','I','II','III','IV','V','VI','VII','VIII','IX'],
['','X','XX','XXX','XL','L','LX','LXX','LXXX','XC'],
['','C','CC','CCC','CD','D','DC','DCC','DCCC','CM'],
['','M','MM','MMM']]
if n<1 or n>3999:
return ''
return decimal_to_roman2(n//10,level+1)+mapping[level][n%10]

def roman_to_decimal(s):
values = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
total = 0
for i in range(len(s)):
if i<len(s)-1 and values[s[i]]<values[s[i+1]]:
total -= values[s[i]]
else:
total += values[s[i]]

# error checking
for i in range(1,4000):
x = decimal_to_roman2(i,0)
y = roman_to_decimal(x)
if i != y:
print('error when transforming', i,':',x,y)

###6ο Εργαστήριο

def line_rec(n):
if n == 1:
return [1]
else:
line = [1]
previous_line = line_rec(n-1)
for i in range(len(previous_line)-1):
line.append(previous_line[i] + previous_line[i+1])
line += [1]
return line

def line_rec2(n):
if n == 1:
return [1]
else:
p_line = line_rec(n-1)
line = [p_line[i]+p_line[i+1] for i in range(len(p_line)-1)]
line.insert(0,1)
line.append(1)
return line

def line(n):
previous_line = [1]
while n>1:
line = [1]
for i in range(len(previous_line)-1):
line.append(previous_line[i] + previous_line[i+1])
line.append(1)
previous_line = line
n-=1
return previous_line

def fib(n):
tri = []
for i in range(1,n+1):
tri.append(line(i))
sum = 0
for i in range((n-1),n//2-1,-1):
sum += tri[i][(n-1)-i]
return sum

n = int(input("Input the line number: "))
print(line_rec(n))
print(line_rec2(n))
print(line(n))
for i in range(1,16):
print(fib(i))

###7ο Εργαστήριο

from string import ascii_letters as A
def strip_punc(s):
l = list(s)
while len(l)>0 and l[0] not in A:
del l[0]
while len(l)>0 and l[-1] not in A:
del l[-1]
return ''.join(l)

def count_lens(text):
l = text.split()
d = {}
for s in l:
s = strip_punc(s)
if len(s) not in d:
d[len(s)]=1
else:
d[len(s)]+=1
return sorted(d.items())
print(l)
for x,y in l:
print("%d words have length %d" % (y,x))
f.close()

###8ο Εργαστήριο

t = open('stats.txt','w')
f = open('Sacramentorealestatetransactions.csv')
s = 0
d = {}
sa = {}
for l in f:
data = l.split(',')
if int(data[6])>=2000:
s += int(data[9])
type = data[7]
if type in d:
d[type]+=1
sa[type]+=int(data[6])
else:
d[type]=1
sa[type]=int(data[6])

t.write('Total value of transactions of properties at least 2000 sq.f.: '+str(s)+'\n')
t.write('Total number of transactions per property type:\n')
for i in d:
t.write('\t%s: %d\n' % (i,d[i]))
t.write('Average size of sold properties per property type:\n')
for i in sa:
t.write('\t%s: %.2f\n' % (i,sa[i]/d[i]))
t.close()
```

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.