Python zero basic programming [76-100]

Posted by cool75 on Tue, 01 Feb 2022 00:34:47 +0100

This column mainly involves 100 Python zero basic programming questions, most of which are translated on Github 100+ Python challenging programming exercises , if you don't have dyslexia in English, you can go to Github to read. There are 100 questions in the column, which is divided into four chapters with 25 questions in each chapter; This blog is the fourth article [76-100] of Python zero basic programming; Well, no more nonsense, start to expand;

Part I: Python zero basic programming [1-25]

Part II: Python zero basic programming [26-50]

Part III: Python zero basic programming [51-75]

Part IV: Python zero basic programming [76-100]

Question 76

Question: please write a program to output random even numbers between O and 10. Use random modules and lists to understand.
Tip: use random. For random elements in the list choice().

import random
print(random.choice([i for i in range(11) if i%2==0]))

Question 77

Question: please write a program to output a random number, which can be divided by 5 and 7. Between 0 and 10, use random modules and lists to understand.
Tip: use random. For random elements in the list choice().

import random
print(random.choice([i for i in range(201) if i%5==0 and i%7==0]))

Question 78

Question: please write a program to generate a list of 5 random numbers between 100 and 200.
Tip: use random Sample() generates a list of random values.

import random
print(random.sample(range(100), 5))

Question 79

Question: please write a program to randomly generate a list containing 5 even numbers between 100 and 200.
Tip: use random Sample() generates a list of random values.

import random
print(random.sample([i for i in range(100,201) if i%2==0], 5))

Question 80

Question: please write a program to randomly generate a list, from 1 to 1000 (including 1000), with 5 numbers, which can be divided by 5 and 7.
Tip: use random Sample() generates a list of random values.

import random
print(random.sample([i for i in range(1,1001) if i%5==0 and i%7==0], 5))

Question 81

Question: please write a program to randomly print an integer between 7 and 15 (including 15).
Tip: use random Randrange() to a random integer within a given range.

import random
print(random.randrange(7,16))

Question 82

Question: please write a program to compress and decompress the string "hello world!hello world!hello world!hello world!".
Tip: use zlib Compress () and zlib Decompress() to compress and decompress strings.

import zlib
s = b'hello world!hello world!hello world!hello world!'
t = zlib.compress(s)
print(t)
print(zlib.decompress(t))

Question 83

Question: please write a program to print the running time of "1 + 1" execution 100 times.
Tip: use the timeit() function to measure run time.

from timeit import Timer
t = Timer("for i in range(100):1+1")
print(t.timeit())

Question 84

Question: please write a program to shuffle and print the list [3,6,7,8].
Tip: use the shuffle() function to shuffle the list.

from random import shuffle
li = [3,6,7,8]
shuffle(li)
print(li)

Question 85

Question: please write a program to shuffle and print the list [3,6,7,8].
Tips: shuffle the shuffle function list.

from random import shuffle
li = [3,6,7,8]
shuffle(li)
print(li)

Question 86

Question: please write a program to generate all sentences with subject in ["I", "You"], verb in ["Play", "Love"] and object in ["Hockey","Football"]
Tip: use the list[index] notation to get elements from the list.

subjects=["I", "You"]
verbs=["Play", "Love"]
objects=["Hockey","Football"]
for i in range(len(subjects)):
    for j in range(len(verbs)):
        for k in range(len(objects)):
            sentence = "%s %s %s." % (subjects[i], verbs[j], objects[k])
            print(sentence)

Question 87

Question: please write a program to print the list and delete the even number after deletion [5,6,77,45,22,12,24].
Tip: use list understanding to remove a set of elements from a list.

li = [5,6,77,45,22,12,24]
li = [x for x in li if x%2!=0]
print(li)

Question 88

Question: to understand using the list, please write a program and print the list after deleting the deletion number that can be divided by 5 and 7 in [12,24,35,70,88120155].
Tip: use list understanding to remove a set of elements from a list.

li = [12,24,35,70,88,120,155]
li = [x for x in li if x%5!=0 and x%7!=0]
print(li)

Question 89

Question: using the list understanding method, please write a program to print the list after removing the elements at positions 0, 2, 4 and 6 in [12, 24, 35, 70, 88120155].
Tip: use list understanding to remove a set of elements from a list. Use enumerate() to get (index, value) tuples.

li = [12,24,35,70,88,120,155]
li = [x for (i,x) in enumerate(li) if i%2!=0]
print(li)

Question 90

Question: using list understanding, write a program to generate a 358 three-dimensional array with each element of 0.
Tip: use list understanding to create arrays.

array = [[ [0 for col in range(8)] for col in range(5)] for row in range(3)]
print(array)

Question 91

Question: using the list understanding, please write a program to remove the 0, 4 and 5 numbers in [12,24,35,70,88120155] and print the list.
Tip: use list understanding to remove a set of elements from a list. Use enumerate() to get (index, value) tuples.

li = [12,24,35,70,88,120,155]
li = [x for (i,x) in enumerate(li) if i not in (0,4,5)]
print(li)

Question 92

Question: by using the list understanding, please write a program to print the list after deleting the value 24 in [12,24,35,24,88120155].
Tip: use the remove method of the list to delete a value.

li = [12,24,35,24,88,120,155]
li = [x for x in li if x!=24]
print(li)

Question 93

Problem: for two known linked lists [1,3,6,78,35,55] and [12,24,35,24,88120155], write a program to generate a linked list whose elements are the intersection of the above two linked lists.
Tip: use set() and "& =" to intersect sets.

set1=set([1,3,6,78,35,55])
set2=set([12,24,35,24,88,120,155])
set1 &= set2
li=list(set1)
print(li)

Question 94

Problem: for a given list [12,24,35,24,88120155,88120155], write a program to print the list - delete all duplicate values and keep the original order.
Tip: use set() to store values that do not have duplicates.

def removeDuplicate( li ):
    newli=[]
    seen = set()
    for item in li:
        if item not in seen:
            seen.add( item )
            newli.append(item)
    return newli
li=[12,24,35,24,88,120,155,88,120,155]
print(removeDuplicate(li))

Question 95

Problem: define a class Person and its two subclasses: Male and Female. All classes have a method "getGender", which can print "Male" as Male class and "Female" as Female class.
Tip: use parentclass to define subclasses.

class Person(object):
    def getGender( self ):
        return "Unknown"
class Male( Person ):
    def getGender( self ):
        return "Male"
class Female( Person ):
    def getGender( self ):
        return "Female"
aMale = Male()
aFemale= Female()
print(aMale.getGender())
print(aFemale.getGender())

Question 96

Question: please write a program to calculate and print the number of each character in the string entered by the console.
Example: if the following string is used as the input of the program: abcdefgab;
Then, the output of the program should be: a,2 c,2 b,2 e,1 d,1 g,1 f,1;
Tip: use dict to store key / value pairs. Use the dict.get() method to find the key with the default value.

dic = {}
s=input()
for s in s:
    dic[s] = dic.get(s,0)+1
print('\n'.join(['%s,%s' % (k, v) for k, v in dic.items()]))

Question 97

Question: please write a program to receive a string from the console and print it in reverse order.
Example: if the following string is used as the input of the program: rise to vote sir;
Then, the output of the program should be: ris etov ot esir;
Tip: use list[::-1] to iterate over a list in reverse order.

s=input()
s = s[::-1]
print(s)

Question 98

Question: please write a program to receive a string from the console and print characters with even index;
Example: if the following string is used as the input of the program: H1e2l3l4o5w6o7r8l9d
Then, the output of the program should be: Helloworld;
Tip: use list[:2] to iterate over the list in step 2.

s=input()
s = s[::2]
print(s)

Question 99

Question: please write a program to print all the permutations of [1,2,3];
Tip: use itertools Permutations) to get the arrangement of the list.

import itertools
print(list(itertools.permutations([1,2,3])))

Question 100

Problem: write a program to solve a classic problem in ancient China: we count 35 heads and 94 legs of chickens and rabbits on the farm. How many rabbits and chickens do we have?
Tip: use the for loop to iterate through all possible solutions.

def solve(numheads,numlegs):
    ns='No solutions!'
    for i in range(numheads+1):
        j=numheads-i
        if 2*i+4*j==numlegs:
            return i,j
    return ns,ns

numheads=35
numlegs=94
solutions=solve(numheads,numlegs)
print(solutions)

Well, so far, the fourth [76-100] question of 100 questions of zero basic programming in Python has ended. Let's practice hard, think for ourselves first, and look at the answers when we can't do it. Only in this way can we improve faster! If you have any questions, please leave a message in the comment area. I will continue to improve it!

It's all here. Are you sure you don't leave anything, hee hee~

                                     

 

Topics: Python