2021年5月27日 星期四

分組累積例 python list

 s = [int(i) for i in input().split()]


start = 0

end = 0


while end<len(s):

acc =0

while s[start]*s[end]>0 or s[start]==0 and s[end]==0:

acc+=s[end]

end+=1

if end >=len(s): break

end = end -1

if acc>0: print(start+1,end+1,end-start+1,acc)

start = end+1

end = end+1

acc = 0

2021年5月26日 星期三

dict and multi key list sorting

 s = input().lower()

s =s.replace('.','').replace(',','').split()


d = {}

for i in s:

if i in d:

d[i]+=1

else :

d[i]=1

list1 = []

for k,v in d.items():

t = [k,v]

list1.append(t)


for i in range(len(list1)):

for j in range(i+1,len(list1)):

if list1[i][1]<list1[j][1]: list1[i],list1[j]=list1[j],list1[i]

elif list1[i][1]==list1[j][1] and  list1[i][0]>list1[j][0]:  list1[i],list1[j]=list1[j],list1[i]

for i in list1:

print(i[0],i[1])

2021年5月18日 星期二

Graph in Dict structure

 d ='''A -> B

    A -> C

    B -> C

    B -> D

    C -> D

    D -> C

    E -> F

    F -> C'''

e = [i.strip() for i in d.split('\n')]

v = set()

for i in e:

    for j in i:

        if j>='A':

            v.add(j)

v = list(v)

v.sort()


print(e)

print(v)


g = {}

for i in v:

    t = []

    for j in e:

        if i in j:

            for k in j:

                if k>='A' and k<='Z':

                    if k not in i:

                        t.append(k)

    g[i]=t

print(g)

2021年5月13日 星期四

dfs bfs python code

 def dfs(visited,graph,node):

    if node not in visited:

        global d

        d+=1

        print(node,end='->')

        visited.add(node)

        for neighbor in graph[node]:

            dfs(visited,graph,neighbor)

            

def bfs(visited,graph,node):

    visited.append(node)

    queue.append(node)

    while queue:

        s = queue.pop(0)

        print(s,end='->')

        for neighbor in graph[s]:

            if neighbor not in visited:

                visited.append(neighbor)

                queue.append(neighbor)


a = '''8

0 1

0 2

0 3

7 0

1 4

1 5

3 6'''


# create graph

a = a.split('\n')

a.pop(0)


v =set() 

e = []

for i in a:

    t = i.split(' ') 

    print(t)

    e.append(t)

    for j in t:

        v.add(j)

print()


v = list(v)

v.sort()


g ={}

for i in v:

    t =set()

    for j in e:

        if i in j:

            for k in j:

                if k!= i:

                    t.add(k)

    g[i] = t


for x,y in g.items():

    print(x,y)

print()


# dfs 

d = 0

for node in v: 

    visited=set()

    d = 0

    dfs(visited,g,node)

    print(':',d)

print()


# bfs 

for node in v:

    queue = []

    visited = []

    bfs(visited,g,node)

    print()

print()


#  = = = = =

Output:

['0', '1']

['0', '2']

['0', '3']

['7', '0']

['1', '4']

['1', '5']

['3', '6']


0 {'3', '2', '7', '1'}

1 {'0', '5', '4'}

2 {'0'}

3 {'0', '6'}

4 {'1'}

5 {'1'}

6 {'3'}

7 {'0'}


0->3->6->2->7->1->5->4->: 8

1->0->3->6->2->7->5->4->: 8

2->0->3->6->7->1->5->4->: 8

3->0->2->7->1->5->4->6->: 8

4->1->0->3->6->2->7->5->: 8

5->1->0->3->6->2->7->4->: 8

6->3->0->2->7->1->5->4->: 8

7->0->3->6->2->1->5->4->: 8


0->3->2->7->1->6->5->4->

1->0->5->4->3->2->7->6->

2->0->3->7->1->6->5->4->

3->0->6->2->7->1->5->4->

4->1->0->5->3->2->7->6->

5->1->0->4->3->2->7->6->

6->3->0->2->7->1->5->4->

7->0->3->2->1->6->5->4->


dfs Sample

 def dfs(visited,graph,node):

    if node not in visited:

        global d

        d+=1

        print(node,end='->')

        visited.add(node)

        for neighbor in graph[node]:

            dfs(visited,graph,neighbor)


a = '''8

0 1

0 2

0 3

7 0

1 4

1 5

3 6'''


# create graph

a = a.split('\n')

a.pop(0)


v =set() 

e = []

for i in a:

    t = i.split(' ') 

    print(t)

    e.append(t)

    for j in t:

        v.add(j)

print()


v = list(v)

v.sort()


g ={}

for i in v:

    t =set()

    for j in e:

        if i in j:

            for k in j:

                if k!= i:

                    t.add(k)

    g[i] = t


for x,y in g.items():

    print(x,y)

print()


# each node process    

d = 0

for node in v: 

    visited=set()

    d = 0

    dfs(visited,g,node)

    print(':',d)


#   ====

Output:

['0', '1']

['0', '2']

['0', '3']

['7', '0']

['1', '4']

['1', '5']

['3', '6']


0 {'1', '3', '7', '2'}

1 {'5', '4', '0'}

2 {'0'}

3 {'0', '6'}

4 {'1'}

5 {'1'}

6 {'3'}

7 {'0'}


0->1->5->4->3->6->7->2->: 8

1->5->4->0->3->6->7->2->: 8

2->0->1->5->4->3->6->7->: 8

3->0->1->5->4->7->2->6->: 8

4->1->5->0->3->6->7->2->: 8

5->1->4->0->3->6->7->2->: 8

6->3->0->1->5->4->7->2->: 8

7->0->1->5->4->3->6->2->: 8

2021年5月8日 星期六

前n個質數

 def p(n):

    c = 0

    for i in range(1,n+1):

        if n%i==0: c+=1

    if c==2:

        return True

    else:

        return False

        

n = 10

i = 2 

c = 0

while c < n:

    if p(i):

        print(c+1,':',i)

        c+=1

    i+=1


print()

c=0

for i in range(1,100):

    if p(i) :

        print(c+1,':',i)

        c+=1

    if c>=10:

        break

python list comprehension & dict

 a = '''

Tomorrow Will Be Better

The Holdup

At first he was sly

He was opening doors

Making sure that you know you're adored

Making out in his Honda Accord

You never felt this way before

But things change since the first date

Think back on when he was great

So sad 'cause he's always late

Lose time not pounds when you're overwaiting

You're overdating

Think man is satan

Growing impatient

You're done participating

You think you lost him

But never had him

I'm sorry that you think you're permanently damaged

I promise it gets better if you wait so

Don't give up on that boy who's got a halo

You opened up your heart to have it broken

Don't let it scar you so bad you can't leave it open

The bar will always be there when you need it

But taking shots won't help erase how you were treated

I promise it gets better if…

'''


# a = a.replace("'","")

ab = a


#sol 1

a = [i.lower() for i in a.split() if "'" not in i]

s = list(set(a))

s.sort()

for i in s:

    print(str(i)+':'+str(a.count(i)))

print()


#sol 2

   

wCounts = {}

for w in a:

    if w.lower() in wCounts:

        wCounts[w.lower()] += 1

    else:

        wCounts[w.lower()]=1

k_list = list(wCounts.keys())

k_list.sort()


for k in k_list:

    print(k,':',wCounts[k])


output ----------------------------------

a:1

accord:1

adored:1

always:2

at:1

back:1

bad:1

bar:1

be:2

before:1

better:3

boy:1

broken:1

but:3

change:1

damaged:1

date:1

done:1

doors:1

erase:1

felt:1

first:2

gets:2

give:1

got:1

great:1

growing:1

had:1

halo:1

have:1

he:3

heart:1

help:1

him:2

his:1

holdup:1

honda:1

how:1

i:2

if:1

if…:1

impatient:1

in:1

is:1

it:6

know:1

late:1

leave:1

let:1

lose:1

lost:1

making:2

man:1

need:1

never:2

not:1

on:2

open:1

opened:1

opening:1

out:1

overdating:1

overwaiting:1

participating:1

permanently:1

pounds:1

promise:2

sad:1

satan:1

scar:1

shots:1

since:1

sly:1

so:3

sorry:1

sure:1

taking:1

that:3

the:3

there:1

things:1

think:4

this:1

time:1

to:1

tomorrow:1

treated:1

up:2

wait:1

was:3

way:1

were:1

when:3

will:2

you:11

your:1

python dict & list comprehension sample

 a = '''

Tomorrow Will Be Better

The Holdup

At first he was sly

He was opening doors

Making sure that you know you're adored

Making out in his Honda Accord

You never felt this way before

But things change since the first date

Think back on when he was great

So sad 'cause he's always late

Lose time not pounds when you're overwaiting

You're overdating

Think man is satan

Growing impatient

You're done participating

You think you lost him

But never had him

I'm sorry that you think you're permanently damaged

I promise it gets better if you wait so

Don't give up on that boy who's got a halo

You opened up your heart to have it broken

Don't let it scar you so bad you can't leave it open

The bar will always be there when you need it

But taking shots won't help erase how you were treated

I promise it gets better if…

'''

#sol 1

ab = a

a = [i.lower() for i in a if i.lower()>='a' and i.lower() <='z' ]

s = list(set(a))

s.sort()

for i in s:

    print(str(i)+':'+str(a.count(i)))

 

print()

#sol 2

ab = [i.lower() for i in ab if i.lower() >= 'a' and i.lower()<='z' ]   

charCounts = {}

for char in ab:

    if char.lower() in charCounts:

        charCounts[char.lower()] += 1

    else:

        charCounts[char.lower()]=1

key_list = list(charCounts.keys())

key_list.sort()


for key in key_list:

    print(key,':',charCounts[key])