Python basic 100 questions punch in Day2

Day2

Topic 4

Write a program to accept a series of comma separated numbers from the console and generate a list and a tuple containing each number. Suppose the following inputs are provided to the program:
34,67,55,33,12,98
The output should be:
['34', '67', '55', '33', '12', '98']
('34', '67', '55', '33', '12', '98')

code implementation

lst = input('Please enter an array:').split(",")  #The input is the pattern of string The split() function can remove the specified character
print(lst)
tpl = tuple(lst)  #The tuple() function can convert other types of variables into tuples
print(tpl)

Operation results

Please enter array: 1,2,3,4,5,6,7,8,9,122
['1', '2', '3', '4', '5', '6', '7', '8', '9', '122']
('1', '2', '3', '4', '5', '6', '7', '8', '9', '122')

Topic 5

Define a class with at least two methods: getString;
Get the string from the console – printString;
Print the string in uppercase. Please also include simple test functions to test class methods.

code implementation

class string():
    def __init__(self):
        pass
    def get_string(self):
        self.s = input('Please enter characters')
    def print_string(self):
        print(self.s.upper())
        
str1 = string()
str1.get_string()
str1.print_string()

Operation results

Please enter characters abcdf
ABCDF

Topic 6

Write a program to calculate and print the value according to the given formula:
Q = Square root of [(2 C D)/H]
The fixed values of C and H are as follows:
c = 50
h = 30
D is a variable whose value should be entered into your program in comma separated order. For example, let's assume that the program has the following comma separated input sequences:
100,150,180
The output of the program should be:
18,22,24

code implementation

import math
C, H = 50, 30
lst1 = []

def SQ(D):
    return math.sqrt((2*C*D)/H)
lst = input('Please enter an array').split(",")

print(lst)
for i in range(len(lst)):
    lst1.append(int(SQ(int(lst[i]))))

print(lst1)

Operation results

Please enter array 200,300,400
['200', '300', '400']
[25, 31, 36]

Title VII

Write a program to 2 digits, X, Y as input, and generate a two-dimensional array
The element value in the first row and column j of the array should be Ij
*Note: i=0, 1, X-1; j=0, 1, "Y-1". Assuming that the program input is as follows: 3, 5, the program output should be:
[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]

code implementation

x, y =map(int,input("Please enter x,y Value of").split(','))
lst = []
for i in range(x):
    stl = []
    for j in range(y):
        stl.append(i * j)#Addition of list elements
    lst.append(stl)
print(lst)

or

x, y = map(int,input("Please enter x,y Value of").split(","))
lst1 = [[i*j for j in range(y)] for i in range(x)]#The first loop represents columns and the second is rows
print(lst1)

Operation results

Please enter x,y Value of 4,6
[[0, 0, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5], [0, 2, 4, 6, 8, 10], [0, 3, 6, 9, 12, 15]]
Please enter x,y Value of 4,6
[[0, 0, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5], [0, 2, 4, 6, 8, 10], [0, 3, 6, 9, 12, 15]]

Topic 8

Write a program that accepts comma separated word sequences as input, and print words in comma separated order after sorting alphabetically.
Suppose the following inputs are provided to the program:
without,hello,bag,world
Output is:
bag,hello,without,world

code implementation

Method 1

lst = input("Please enter a phrase").split(',')
lst.sort()  #The sort function can sort the list directly
print(','.join(lst))

Method 2

lst = input("Please enter a phrase").split(',')
lst1 = lst[ : ]  #Assign the original list to the new list by slicing
lst1.sort()
print(','.join(lst1))

Method 3

lst = input("Please enter a phrase").split(',')
lst1 = sorted(lst)  #Generate a new list while retaining the original list
print(','.join(lst1))

Operation results

Please enter a phrase without,hello,bag,world
bag,hello,without,world
 Please enter a phrase without,hello,bag,world
bag,hello,without,world
 Please enter a phrase without,hello,bag,world
bag,hello,without,world

Topic 9

Write a program that accepts a sequence of lines as input and prints lines after capitalization of all characters in a sentence.
Suppose the following inputs are provided to the program:
Hello world
Practice makes perfect
Output is:
HELLO WORLD
PRACTICE MAKES PERFECT

code implementation

Method 1

def userinput():
    while True:
        s = input()
        if not s:
            return  #At this time, return without anything is equivalent to returning none, and the program will stop
        yield s   #It is equivalent to return and tells the program that the next execution starts from here

for line in map(str.upper, userinput()):  #Batch function processing
    print(line)

Method 2

lst = []

while input():
    x = input()
    if len(x) == 0:
        break
    lst.append(x.upper())

for line in lst:
    print(line)

Operation results

Hello world
HELLO WORLD
Practice makes perfect
PRACTICE MAKES PERFECT
asdfg
ASDFG

Keywords: Python string list

Added by jd307 on Tue, 01 Feb 2022 11:07:31 +0200