This article lists and summarizes the ones I often used in Paiza's skill check.
It is mainly supposed to solve D and C rank problems, so I hope it will be helpful.
By the way, I'm B rank, so I don't think it will be helpful for people of the same level or higher.
If you are new to Paiza in the first place, please refer to this article. Python Paiza-Various skill checks and standard input (input)
The first thing to do when solving Paiza's problem is to get the required values from the example input. Use ʻinput` for that.
ʻInput fetches the value in the input as a string type (
str). Therefore, if you want to convert it to a number, use ʻint (input ())
to convert it to a number.
Also, ʻinput ()` fetches the entire line.
#Enter a b c.
paiza = input()
print(paiza)
#abc
print(type(paiza)) #Check the type of the variable paiza
#<class 'str'> (String type)
#Input example'Hello'
paiza = input() #Store the value in the paiza variable#You only have to copy here!
print(paiza) #Hello
Be careful not to forget the ()
.
I got one value earlier, but what should I do when I get multiple values?
#Enter a b c.
paiza = input().split() #You only have to copy here!
print(paiza)
#['a', 'b', 'c']
print(type(paiza))
#<class 'list'>
You can get it as a list like this. After that, it seems that you can use it well if you use a for statement etc.
Earlier I introduced how to retrieve when there are multiple values in one line, but what should I do if I want to retrieve each value one by one instead of a list?
#Type a b
paiza, skill = input().split() #You only have to copy here!
print(paiza)
#a
print(skill)
#b
print(type(paiza))
#<class 'str'>
print(type(skill))
#<class 'str'>
By doing this, you can get the values by assigning them to variables one by one. However, be aware that an error will occur if the number of variables is not enough for the number of values in one line.
#Type a b c
paiza, skill = input().split() #Value a, b,The variable is paiza for the three of c,Not enough because there are only two skills
#ValueError: too many values to unpack (expected 2)
Not enough! I was angry.
As mentioned earlier, ʻinput` takes a value as a string. So how to make numbers?
#Enter 1
paiza = input()
print(paiza)
#1
print(type(paiza))
#<class 'str'>
paiza_number = int(paiza) #You only have to copy here!
#int()Use to make numbers
print(type(paiza_number))
#<class 'int'>
Now let's see how to convert multiple values into numbers. We use list comprehension notation.
#Enter 1 and 2
number1, number2= [int(x) for x in input().split()] #You only have to copy here!
print(number1)
#1
print(number2)
#2
print(type(number1))
#<class 'int'>
print(type(number2))
#<class 'int'>
I think it's a little difficult, so I'll explain it.
ʻInput (). split ()contains 1 and 2 as a list. Take out the elements of the list one by one with the for statement and assign them to
x. Then convert that
x to a number with ʻint (x)
and substitute it for number1
.
If you want to know more details, please check it out.
Apart from this method, there is also a method using map
.
This is shorter so it may be easier to use.
#Enter 1 2 3
number1, number2, number3 = map(int, input().split()) #You only have to copy here!
print(a)
#1
print(b)
#2
print(c)
#3
print(type(number1))
#<class 'int'>
print(type(number2))
#<class 'int'>
print(type(number3))
#<class 'int'>
I use the list all the time. We'll show you how to add and remove lists, not just prepare them.
paiza = []
This can also be used to empty the list.
#List name.append(Value you want to add)
paiza.append('a')
print(paiza)
#['a']
remove
print(paiza)
#['a', 'b', 'c']
#List name.remove(Element name)
paiza.remove('a')
print(paiza)
#['b', 'c']
del
#del list name[Index number]
print(paiza)
#['a', 'b', 'c']
del paiza[1]
print(paiza)
#['a', 'c']
Please note that the list below will be sorted in this case.
paiza = [2,5,3,1,4]
paiza.sort()
print(paiza)
#[1, 2, 3, 4, 5]
paiza = [2,5,3,1,4]
paiza.sort(reverse=True)
print(paiza)
#[5, 4, 3, 2, 1]
reverse
is a specification to reverse the order.
#'Put what you want to separate here'.join(List name)
paiza = ['a', 'b', 'c', 'd', 'e']
print('?'.join(paiza)) #?Separated by
#a?b?c?d?e
Loops are often used in Paiza's skill checks. Please check out how to use the basic for loop.
It's annoying to write ʻinput` many times when retrieving lines of input. In such a case, let's take it out with a for statement.
for i in range(Number of times you want to repeat): #Example of use: range(5)
paiza = input()
By writing like this, ʻinput` can be repeated as many times as the number entered in range.
#a b c d e
paiza = []
for i in range(5):
a = input()
paiza.append(a)
print(paiza)
#['a', 'b', 'c', 'd', 'e']
Take one line of input with ʻinput ()` and add it to the list, repeating 5 times.
a = 10
b = 3
paiza = 10//3
print(paiza)
#3
math.floor
import math
a = 10.123
print(math.floor(a))
#10
import math
a = 10.123
print(math.ceil(a))
#10
Even-numbered and odd-numbered judgments using if statements appear several times.
number = 2
if number%2==0:
print('ok')
else:
print('no')
#ok
It calculates to output too much, and if too much is 0, it outputs ʻok, otherwise it outputs
no`.
I've listed all the things I can think of and often use. We plan to update it from time to time.
Recommended Posts