Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

Table of Contents

Cvičení 4

04a

Pole

Příkazy z videa:

matematika=[1,1,2,1,4,2,1,3,1,2]
len(matematika)
matematika[0]
matematika[9]
matematika[10]
matematika[-1]
matematika[-10]
matematika[-11]
matematika.append(5)
matematika=matematika+[4,3,4]
matematika[2:6]
matematika[2:14]
matematika[0:7]+[6,7,8]+matematika[7:14]
matematika[:7]+[6,7,8]+matematika[7:]

04b

Načítání a práce s polem

Příkazy na vytváření a načítání pole:

n=10
pole=[0]*n
pole2=[]
for i in range(n):
  pole2.append(n-i)
pole3=[ n-i for i in range(n)]
pole4=[ n-i for i in range(n) for j in range(3) ]
pole_f=list(map(float,input().split()))
extrem.py:
pole = list(map(int, input().split()))

def nej(p):
    if len(p)==0:
        minimum = None
        maximum = None
    else:
        minimum = p[0]
        maximum = p[0]
    for i in range(len(p)):
        if p[i]<minimum:
            minimum = p[i]
        if p[i]>maximum:
            maximum = p[i]
    return minimum, maximum

maly, velky = nej(pole)
print(maly,velky)
extrem = nej(pole)
print(extrem[0], extrem[1])
extrem2.py:
pole = list(map(int, input().split()))

def nej(p):
    minimum = None
    maximum = None
    for i in p:
        if minimum==None or i<minimum:
            minimum = i
        if maximum==None or i>maximum:
            maximum = i
    return minimum, maximum

maly, velky = nej(pole)
print(maly,velky)
extrem = nej(pole)
print(extrem[0], extrem[1])

04c

Retězce

příklady řetězců:

s1="text"
s2='text'
s3='''dlouhy
text'''
s4="""take
dlouhy text s " i '
konec"""
s5="""Sileny retezec '''\"\"\""""
print(s1,s2,s3,s4,s5)
s6='\''
print(s6)
s='popokatepetl'
len(s)
s[2:7]
for i in s:
  print(i, end='-')
print()
c='h'
chr(ord(c)-ord('a')+ord('A'))

palindrom.py:

def palindrom(s):
    r = True
    i=0
    j=len(s)-1
    while i<j:
        if s[i]!=s[j]:
            r = False
            break
        i+=1
        j-=1
    return r

s='kobylamamalybok'
s2="kobyla"
print(s,palindrom(s))
print(s2,palindrom(s2))
palindrom2.py:
def to_small(c):
    if c>='A' and c<='Z':
        c = chr(ord(c)-ord('X')+ord('x'))
    return c

def palindrom(s):
    r = False
    i=0
    j=len(s)-1
    while not (s[i].isalpha()) and i<j:
        i+=1
    while not (s[j].isalpha()) and i<j:
        j-=1

    while i<j:
        if to_small(s[i])!=to_small(s[j]):
            r = False
            break
        else:
            r = True
        i+=1
        while not (s[i].isalpha()) and i<j:
            i+=1
        j-=1
        while not (s[j].isalpha()) and i<j:
            j-=1
        
    return r

s='Kobyla ma maly bok.'
s2="kobyla"
s3="+-/*"
print(s,palindrom(s))
print(s2,palindrom(s2))
print(s3,palindrom(s3))

courses/b3b33alp/cviceni/kratka_videa/c04.txt · Last modified: 2021/09/24 21:57 by stepan