0% found this document useful (0 votes)
82 views

Python BuiltInList

The document defines functions that use various Python collection objects: - func1 uses product to generate the Cartesian product of iterables. - func2 uses permutations to generate all permutations of an iterable. - func3 uses combinations to generate all unique combinations of iterables. - func4 uses combinations_with_replacement to allow individual elements to appear multiple times. - func5 uses accumulate to apply a function cumulatively to elements in an iterable.

Uploaded by

roy.scar2196
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views

Python BuiltInList

The document defines functions that use various Python collection objects: - func1 uses product to generate the Cartesian product of iterables. - func2 uses permutations to generate all permutations of an iterable. - func3 uses combinations to generate all unique combinations of iterables. - func4 uses combinations_with_replacement to allow individual elements to appear multiple times. - func5 uses accumulate to apply a function cumulatively to elements in an iterable.

Uploaded by

roy.scar2196
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

import os

from collections import *

# Enter your code here.


var1 = namedtuple('player',['name','runs'])
#namedtuple
def func1(x,y):

s = var1 (x, y)
return s
#deque
def func2(s):
lst = []
lst.append(s)
d = deque(lst)
return d
#Counter
def func3(x):
e=Counter(x)
return e
#Ordereddict
def func4(m,n,o,p,q):
d = OrderedDict()
d[1] = m
d[2] = n
d[3] = o
d[4] = p
d[5] = q
return d
#defaultdict
def func5(a,b):
s= defaultdict(list)
s[0] = a
s[1] = b
return s
'''For testing the code, no input is to be provided'''

if __name__ == "__main__":
a,b=input().split()
x=func1(a,b)
print(x)

line = input().split()
x=func2(line)
print(x)

a=list(map(int, input().split()))
x=func3(a)
print(x)

a,b,c,d,e=input().split()
x=func4(a,b,c,d,e)
print(x)

a,b=input().split()
x=func5(a,b)
print(x)

Input (stdin)
Run as Custom Input

dhoni 10000
d e q u e
1 1 2 3 4 5 6 6 6 8
H E L L O
Hello Everyone
Your Output (stdout)
player(name='dhoni', runs='10000')
deque([['d', 'e', 'q', 'u', 'e']])
Counter({6: 3, 1: 2, 2: 1, 3: 1, 4: 1, 5: 1, 8: 1})
OrderedDict([(1, 'H'), (2, 'E'), (3, 'L'), (4, 'L'), (5, 'O')])
defaultdict(<class 'list'>, {0: 'Hello', 1: 'Everyone'})
Expected Output

Download
player(name='dhoni', runs='10000')
deque([['d', 'e', 'q', 'u', 'e']])
Counter({6: 3, 1: 2, 2: 1, 3: 1, 4: 1, 5: 1, 8: 1})
OrderedDict([(1, 'H'), (2, 'E'), (3, 'L'), (4, 'L'), (5, 'O')])
defaultdict(<class 'list'>, {0: 'Hello', 1: 'Everyone'})

import os
from itertools import *

# Enter your code here.

#product
def func1(w,x,y,z):
a = []
a.append(w)
a.append(x)
b = []
b.append(y)
b.append(z)
prod = product(a, b, repeat = 2)
return(list(prod))

#permutation
def func2(x,y,z):
a=[]
a.append(x)
a.append(y)
a.append(z)
perm = permutations(a)
return(list(perm))

#combination
def func3(x,y,z):
a = []
a.append(x)
a.append(y)
a.append(z)
comb = combinations(a,2)
return(list(comb))

#combination with replacement


def func4(x,y,z):
a = []
a.append(x)
a.append(y)
a.append(z)
comb_wr = combinations_with_replacement(a,2)
return(list(comb_wr))

#accumulate
def func5(m,n,o,p,q):
a = []
a.append(m)
a.append(n)
a.append(o)
a.append(p)
a.append(q)
accum = accumulate(a,min)
return(list(accum))

'''For testing the code, no input is to be provided'''

if __name__ == "__main__":
a,b,c,d=list(map(int, input().split()))
x=func1(a,b,c,d)
print(x)

a,b,c=list(map(int, input().split()))
x=func2(a,b,c)
print(x)

a,b,c=list(map(int, input().split()))
x=func3(a,b,c)
print(x)

a,b,c=list(map(int, input().split()))
x=func4(a,b,c)
print(x)

a,b,c,d,e=list(map(int, input().split()))
x=func5(a,b,c,d,e)
print(x)

Input (stdin)

Run as Custom Input

5 6 7 8
5 6 9
5 6 9
5 6 9
5 9 6 7 8
Your Output (stdout)
[(5, 7, 5, 7), (5, 7, 5, 8), (5, 7, 6, 7), (5, 7, 6, 8), (5, 8, 5, 7), (5, 8, 5,
8), (5, 8, 6, 7), (5, 8, 6, 8), (6, 7, 5, 7), (6, 7, 5, 8), (6, 7, 6, 7), (6, 7, 6,
8), (6, 8, 5, 7), (6, 8, 5, 8), (6, 8, 6, 7), (6, 8, 6, 8)]
[(5, 6, 9), (5, 9, 6), (6, 5, 9), (6, 9, 5), (9, 5, 6), (9, 6, 5)]
[(5, 6), (5, 9), (6, 9)]
[(5, 5), (5, 6), (5, 9), (6, 6), (6, 9), (9, 9)]
[5, 5, 5, 5, 5]
Expected Output

Download
[(5, 7, 5, 7), (5, 7, 5, 8), (5, 7, 6, 7), (5, 7, 6, 8), (5, 8, 5, 7), (5, 8, 5,
8), (5, 8, 6, 7), (5, 8, 6, 8), (6, 7, 5, 7), (6, 7, 5, 8), (6, 7, 6, 7), (6, 7, 6,
8), (6, 8, 5, 7), (6, 8, 5, 8), (6, 8, 6, 7), (6, 8, 6, 8)]
[(5, 6, 9), (5, 9, 6), (6, 5, 9), (6, 9, 5), (9, 5, 6), (9, 6, 5)]
[(5, 6), (5, 9), (6, 9)]
[(5, 5), (5, 6), (5, 9), (6, 6), (6, 9), (9, 9)]
[5, 5, 5, 5, 5]
min

You might also like