[Python] A program that calculates the number of socks to be paired

[Python] A program that calculates the number of socks to be paired

This is a memo for myself.

▼ Question

--There is a list that contains any number of integers. --The values in the list represent the color numbers of the socks. --One set when two of the same color are available. --Calculate the number of sets.

url

▼sample input

python


9
10 20 20 10 10 30 50 10 20

▼sample output

python


3

▼my answer

python


def sockMerchant(n, ar):
    pair=0
    for i in set(ar):
        pair += int(ar.count(i)/2)
    return pair

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')
    n = int(input())
    ar = list(map(int, input().rstrip().split()))
    result = sockMerchant(n, ar)
    fptr.write(str(result) + '\n')
    fptr.close()

** ・ int () ** ** Truncate ** after the decimal point

** ・ count method ** Object.count (i) Returns the number of i's in the object.

It can also be used as a character string.

python


aaa = "Ai Ue Ai Ue Ai"
aaa.count("Ah")

## Keep it simple with comprehensions

▼ my answer (included notation)

python


ar=[10,20,20,10,10,30,50,10,20]

def sockMerchant(n, ar):
    return sum([int((ar.count(i))/2) for i in set(ar)])

sockMerchant(9, ar)

--Substitutions such as "+ =" cannot be used in comprehensions. -In case of [], list type is returned. --sum (list) returns the total value of each element of list

Recommended Posts

[Python] A program that calculates the number of socks to be paired
[Python] A program that counts the number of valleys
[Python] A program that calculates the number of chocolate segments that meet the conditions
[Python] A program to find the number of apples and oranges that can be harvested
[Python] A program that calculates the number of updates of the highest and lowest records
[Python] A program that rotates the contents of the list to the left
[Python] A program that finds the maximum number of toys that can be purchased with your money
[Python] A program that compares the positions of kangaroos.
[Python] A program that finds the shortest number of steps in a game that crosses clouds
[Python] A program that rounds the score
A story that struggled to handle the Python package of PocketSphinx
A script that returns 0, 1 attached to the first Python prime number
[python] A note that started to understand the behavior of matplotlib.pyplot
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
I made a program to check the size of a file in Python
A story about creating a program that will increase the number of Instagram followers from 0 to 700 in a week
A solution to the problem that the Python version in Conda cannot be changed
[Python] A program that calculates the difference between the total numbers on the diagonal line.
python beginners tried to predict the number of criminals
How to get the number of digits in Python
Python Note: The mystery of assigning a variable to a variable
A python script that gets the number of jobs for a specified condition from indeed.com
How to identify the element with the smallest number of characters in a Python list?
A script that can perform stress tests according to the number of CPU cores
A program that summarizes the transaction history csv data of SBI SECURITIES stocks [Python3]
[python] Change the image file name to a serial number
Processing of python3 that seems to be usable in paiza
Write a python program to find the editing distance [python] [Levenshtein distance]
Get the number of specific elements in a python list
[Python] A program that finds the most common bird types
A Python script that compares the contents of two directories
Python tricks: a combination of enumerate () and zip (), checking if a string can be converted to a number, sorting the string as a number
A program that automatically resizes the iOS app icon to the required image size in Python
Summary of points to keep in mind when writing a program that runs on Python 2.5
What seems to be a template of the standard input part of the competition pro in python3
A Python script that allows you to check the status of the server from your browser
The story of writing a program
I made a program in Python that changes the 1-minute data of FX to an arbitrary time frame (1 hour frame, etc.)
The story that the version of python 3.7.7 was not adapted to Heroku
How to determine the existence of a selenium element in Python
A formula that simply calculates the age from the date of birth
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
A function that measures the processing time of a method in python
[Ev3dev] Create a program that captures the LCD (screen) using python
[Python3] Define a decorator to measure the execution time of a function
Try to write a program that abuses the program and sends 100 emails
A program that determines whether a number entered in Python is a prime number
I made a program that automatically calculates the zodiac with tkinter
The story of making a module that skips mail with python
[Python] A simple function to find the center coordinates of a circle
Get the number of readers of a treatise on Mendeley in Python
Create a compatibility judgment program with the random module of python.
Understand the probabilities and statistics that can be used for progress management with a python program
[Python] I tried to make a simple program that works on the command line using argparse.
How to write when you want to put a number after the group number to be replaced with a regular expression in re.sub of Python
[Python] A program that creates stairs with #
[python] [meta] Is the type of python a type?
Set the process name of the Python program
The story of blackjack A processing (python)
A program that plays rock-paper-scissors using Python