Paiza Skill Check List of Frequently Used D and C Ranks ~ Python ~

Introduction

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)

① Standard input using 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.

Features of input ()

ʻ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)

General usage of input ()

#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 ().

When there are multiple values in one line

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.

When there are multiple values in one line, retrieve each value one by one

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.

Make numbers

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'>

List comprehension

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 tox. 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.

map function

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'>

② List

I use the list all the time. We'll show you how to add and remove lists, not just prepare them.

Prepare an empty list

paiza = []

This can also be used to empty the list.

Add element

#List name.append(Value you want to add)
paiza.append('a')
print(paiza)
#['a']

Delete element

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']

sort

Please note that the list below will be sorted in this case.

From the smallest

paiza = [2,5,3,1,4]
paiza.sort()
print(paiza)
#[1, 2, 3, 4, 5]

From the larger

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.

Output list

#'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

③ Loop

Loops are often used in Paiza's skill checks. Please check out how to use the basic for loop.

When retrieving lines of input

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.

Take out what is entered and store it in the list

#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.

④ Decimal point adjustment

Decimal point truncation

Simple division

a = 10
b = 3
paiza = 10//3
print(paiza)
#3

math.floor

import math
a = 10.123
print(math.floor(a))
#10

Round up the decimal point

import math
a = 10.123
print(math.ceil(a))
#10

⑤ Even and odd

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`.

Finally

I've listed all the things I can think of and often use. We plan to update it from time to time.

Recommended Posts

Paiza Skill Check List of Frequently Used D and C Ranks ~ Python ~
List of frequently used built-in functions and methods
Comparison table of frequently used processes of Python and Clojure
List of frequently used Linux commands
[Machine learning] List of frequently used packages
[python] Check the elements of the list all, any
List of main probability distributions used in machine learning and statistics and code in python
Frequently used methods of Selenium and Beautiful Soup
Summary of frequently used Python arrays (for myself)
List of Python code to move and remember
Mayungo's Python Learning Note: List of stories and links
List of Python libraries for data scientists and data engineers
Receives and outputs standard output of Python 2 and Python 3> C implementations
List of Python code used in big data analysis
Display a list of frequently used commands on Zsh
List of python modules
Python: Create a dictionary from a list of keys and values
Solving with Ruby and Python AtCoder ABC138 D Adjacency list
Summary of Python3 list operations
Python --Check type of values
[Python] Frequently used library code
python> Handling of 2D arrays
Check OpenSSL version of python 2.6
Frequently used subpackages of SciPy
Python frequently used code snippets
[Python] Copy of multidimensional list
Python list and tuples and commas
Paiza Python Primer 4: List Basics
Python list comprehensions and generators
Source installation and installation of Python
paiza Skill check past problem collection "C rank equivalent search history"
[Python] A memo of frequently used phrases (by myself) in Python scripts
[Linux] [C / C ++] Operation check memo of fork, thread and thread local variables
[Introduction to Python] I compared the naming conventions of C # and Python.
Solving with Ruby, Python and networkx AtCoder ABC168 D Adjacency list
List of Linear Programming (LP) solvers and modelers available in Python