From a book that programmers can learn (Python): Find the mode

Continuation of Last time This time, the code to find the mode of the array The answer code in C ++ has already been posted, but I rewrote it in python.

test.cpp


int mostFrequent;
int highestFrequency = 0;
int currentFrequency = 0;
for (int i = 0; i < ARRAY_SIZE; i++){
    currentFrequency++;
    if (i == ARRAY_SIZE - 1 || surveyData[i] != surveyData[i + 1]){
        if (currentFrequency > highestFrequency){
            highestFrequency = currentFrequency;
            mostFrequent = surveyData[i];
        }
        currentFrequency = 0;
    }
}

My python code

test37.py


#!/usr/bin/env python
#coding:utf-8

import math

elem = [4,7,3,8,9,7,3,9,9,3,3,10]

###I am fixing it
#count = {Elem.count(v):v for v in set(Elem)}
#high = max(count)
#most = count[high]
elem.sort()
most = 0
high = 0
current = 0
#for i,v in enumerate(Elem):
for i in range(len(elem)):
    current += 1
    if i == len(elem) - 1 or elem[i] != elem[i+1]:
        if current > high:
            high = current
            most = elem[i]
        current = 0
print('Array=',elem, '\n' , 'Mode:',most,'|','Number of times:',high)


・ ・ ・(Terminal execution)
>>> import test37
Array= [3, 3, 3, 3, 4, 7, 7, 8, 9, 9, 9, 10] 
Mode: 3 |Number of times: 4
>>> 


Taking advantage of the previous reflection, I thought about using "list comprehension" and "generator comprehension", but I had to count at the current + = 1 part, so I repeated the above if statement. Did. Even though there is an answer code, it takes time just to change it to python, so I feel that I have to get used to it.

Recommended Posts

From a book that programmers can learn (Python): Find the mode
From a book that programmers can learn ... (Python): Pointer
From a book that programmers can learn ... (Python): About sorting
From a book that programmers can learn (Python): Decoding messages
From a book that programmers can learn ... (Python): Review of arrays
From a book that programmers can learn (Python): Statistical processing-deviation value
From a book that programmers can learn (Python): Conditional search (maximum value)
From a book that programmers can learn (Python): Class declaration (public / private, etc.)
From a book that programmers can learn ... Collect small problem parts
From a book that makes the programmer's way of thinking interesting (Python)
From a book that programmers can learn: Verification of rune checksum (fixed length)
From a book that programmers can learn ... Verification of rune checksum (variable length) Part 2
From a book that programmers can learn: Converting characters that represent numbers to integer types
Find the part that is 575 from Wikipedia in Python
[Python] A program to find the number of apples and oranges that can be harvested
8 services that even beginners can learn Python (from beginners to advanced users)
[Python] A program that rounds the score
Extract lines that match the conditions from a text file with python
I made a simple timer that can be started from the terminal
Find out the name of the method that called it from the method that is python
"Python Kit" that calls a Python script from Swift
[Python] Find the transposed matrix in a comprehension
I made a Docker image that can call FBX SDK Python from Node.js
A python script that draws a band diagram from the VASP output file EIGENVAL
Find the maximum Python
[Python] A program that counts the number of valleys
A python script that gets the number of jobs for a specified condition from indeed.com
Introducing the book "Creating a profitable AI with Python" that allows you to learn machine learning in the shortest course
Python points from the perspective of a C programmer
A programming language that protects the people from NHK
Programmer's way of thinking is from XX book (Python)
A memo that I touched the Datastore with python
"A book that understands Flask from scratch" Reading memo
Programmer's way of thinking is from XX book (Python)
The attitude that programmers should have (The Zen of Python)
A mechanism to call a Ruby method from Python that can be done in 200 lines
[Python] A program that compares the positions of kangaroos.
How to find the first element that matches your criteria in a Python list
A little bit from Python using the Jenkins API
Python --Read data from a numeric data file to find the covariance matrix, eigenvalues, and eigenvectors
A library that monitors the life and death of other machines by pinging from Python
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
A Python script that goes from Google search to saving the Search results page at once
A Python script that allows you to check the status of the server from your browser
A memo that reads data from dashDB with Python & Spark
A Python program in "A book that gently teaches difficult programming"
Find out the apparent width of a string in python
A program that removes specific characters from the entered text
Different from the import type of python. from A import B meaning
Launch a simple WEB server that can check the header
Write a python program to find the editing distance [python] [Levenshtein distance]
python Condition extraction from a list that I often forget
Creating a Python script that supports the e-Stat API (ver.2)
[Python] A program that finds the most common bird types
Iterator that can scan forward from the middle of the sequence
A Python program that aggregates time usage from icalendar data
Find the eigenvalues of a real symmetric matrix in Python
A Python script that compares the contents of two directories
Extract the value closest to a value from a Python list element
Find the maximum python (improved)
Understand the probabilities and statistics that can be used for progress management with a python program