development:python:examples

Differences

This shows you the differences between two versions of the page.


development:python:examples [2020/01/09 07:49] (current) – created - external edit 127.0.0.1
Line 1: Line 1:
 +====== Python examples 9 ======
 +<code python>
 +def zapis():
 +    f=open("rezultat.txt","w")
 +    while True:
 +        a=input("String? ")
 +        if a=="KRAJ":
 +            break
 +        else:
 +            if len(a)<5:
 +                f.write(a + "\n")
 +    f.close()
  
 +    f=open("rezultat.txt","r")
 +    lines=f.readlines()
 +    print("")
 +    print("U datoteku je pohranjeno:")
 +    for line in lines:
 +        print(line, end = '')
 +    f.close()
 +</code>
 +
 +<code python>
 +def ispisNS(ime):
 +    import csv
 +    a=0
 +    with open(ime) as f:
 +        reader=csv.reader(f,delimiter=',')
 +        for line in reader:
 +            for col in line:
 +                if len(col)>a:
 +                    a=len(col)
 +                    b=col
 +    print("Najduzi string je " + str(b) + " s " + str(a) + " znakova.")
 +</code>
 +
 +<code python>
 +def zapisL(f,n):
 +    ret=False
 +    if len(f)<12:
 +        f1=f.split(".")
 +        if f1[0][0:1].isalpha() and f1[1]=="txt":
 +            ret=True
 +            f=open(f,"w")
 +            for a in range(1,n+1):
 +                b=0
 +                c=""
 +                while b<a:
 +                    if len(c)==0:
 +                        c=str(a)
 +                    else:
 +                        c=c + " " + str(a)
 +                    b+=1
 +                f.write(c + "\n")
 +            f.close()
 +    return ret
 +</code>
 +
 +<code python>
 +def pr(qty):
 +    num=0
 +    n=0
 +    c=""
 +    while n<qty:
 +        if num > 1:
 +            for i in range(2,num):  
 +               if (num % i) == 0:  
 +                   break
 +            else:
 +                n+=1
 +                c=c+str(num)+";"
 +        num+=1
 +    return(c)
 +def zapisP(f,n):
 +    ret=False
 +    if len(f)<8:
 +        if "." not in f:
 +            if f[0:1].isalpha():
 +                ret=True
 +                f=open(f+".txt","w")
 +                f.write(pr(n+1) + "\n")
 +                f.close()
 +    return ret
 +</code>
 +
 +<code python>
 +def izr(f):
 +    sa=0
 +    su=0
 +    os=0
 +    uk=0
 +    f=open(f,"r")
 +    text=f.read()
 +    f.close()
 +    for a in text:
 +        uk+=1
 +        if a.isalpha():
 +            if a.lower() in ['a','e','i','o','u']:
 +                sa+=1
 +            else:
 +                su+=1
 +        else:
 +            os+=1
 +    f=open("rezultat.txt","w")
 +    f.write("Samoglasnika je "+str(round(sa/uk*100,2))+"%\n")
 +    f.write("Suglasnika je "+str(round(su/uk*100,2))+"%\n")
 +    f.write("Ostalih znakova je "+str(round(os/uk*100,2))+"%\n")
 +    f.close()
 +</code>
 +
 +<code python>
 +def frek(f):
 +    f=open(f,"r")
 +    lines=f.readlines()
 +    f.close()
 +    newline=""
 +    for line in lines:
 +        for c in line.lower():
 +            if c.isalpha() or c.isspace():
 +                newline=newline+c
 +    words=newline.split()
 +    qty=dict()
 +    for word in words:
 +        if word in qty:
 +            qty[word]+=1
 +        else:
 +            qty[word]=1
 +    import collections
 +    od=collections.OrderedDict(sorted(qty.items()))
 +    f=open("rezultat.txt","w")
 +    for k,v in od.items(): f.write(k+": "+str(v)+"\n")
 +    f.close()
 +</code>
 +
 +<code python>
 +def ar(f):
 +    f=open(f,"r")
 +    lines=f.readlines()
 +    f.close()
 +    a=0
 +    b=0
 +    for line in lines:
 +        a=a+int(line)
 +        b+=1
 +    f=open("rezultat.txt","w")
 +    f.write(str(a/b))
 +    f.close()
 +</code>
 +
 +<code python>
 +def zbr(f):
 +    f=open(f,"r")
 +    lines=f.readlines()
 +    f.close()
 +    a=0
 +    for line in lines:
 +        a=a+int(line)
 +    f=open("rezultat.txt","w")
 +    f.write(str(a))
 +    f.close()
 +</code>
  • development/python/examples.txt
  • Last modified: 2020/01/09 07:49
  • by 127.0.0.1