Run a simple algorithm in Python

Create a function that multiplies each element of the list

Example: input a=[1,2,3] b=[4,5,6]

output ->[4,10,18]

policy

First, create something that works even if it is dirty. After that, refactor if you can afford it. First write the contents, and if it moves, make it a function

a=[1, 2, 3]
b=[4, 5, 6]

[a[0] * b[0], a[1] * b[1], a[2] * b[2]]
#output
[4, 10, 18]

It worked for the time being.

Functionalization 1

def calc(a, b):
  return [a[0] * b[0], a[1] * b[1], a[2] * b[2]]

However, with this, if the number of elements in the list increases or decreases by one, it cannot be dealt with. Code that is not versatile. So refactor it to make it more versatile.

calc_results = []
for i in range(len(a)):
  calc_results.append(a[i] * b[i])

Create a list, multiply it by the length of the list, and store it in the list. With this, you can handle any number of elements in the list.

Functionalization 2

def calc(a,b):
  calc_results = []
  for i in range(len(a)):
    calc_results.append(a[i] * b[i])
  return calc_results

However, this makes the code cluttered, so the next step is to improve readability.

calc_results = []
for (x,y) in zip (a,b):
  calc_results.append(x * y)
calc_results

If you want to get and use elements such as lists and tuples at the same time, specify them in the argument of the zip () function.

Functionalization 3

def calc(a,b):
  calc_results = []
  for (x,y) in zip (a,b):
    calc_results.append(x * y)
  return calc_results

Summary

If you do it individually, I think that even a dirty program can be managed with a spirit of movement for the time being, but if you develop it as a team, I think that it is better to have code that is easy to maintain and has high readability and versatility, so it is best to make something that works first. I think it's important, but maybe you should refactor your code after that.

Recommended Posts

Run a simple algorithm in Python
Implementing a simple algorithm in Python 2
Write A * (A-star) algorithm in Python
A simple HTTP client implemented in Python
Try drawing a simple animation in Python
Run the Python interpreter in a script
Write a simple Vim Plugin in Python 3
A * algorithm (Python edition)
Simple gRPC in Python
Genetic algorithm in python
Algorithm in Python (Bellman-Ford)
Algorithm in Python (Dijkstra's algorithm)
Set up a simple HTTPS server in Python 3
A simple Pub / Sub program note in Python
Create a simple momentum investment model in Python
Take a screenshot in Python
Create a function in Python
Create a dictionary in Python
Run automatic jobs in python
Algorithm in Python (primality test)
Run shell commands in python
Reproduce Euclidean algorithm in Python
Algorithm in Python (binary search)
Make a bookmarklet in Python
Simple regression analysis in Python
Implement Dijkstra's Algorithm in python
Draw a heart in Python
Simple IRC client in python
Write a super simple molecular dynamics program in python
Make a simple Slackbot with interactive button in python
Create a plugin to run Python Doctest in Vim (2)
Create a plugin to run Python Doctest in Vim (1)
A memorandum to run a python script in a bat file
2. Multivariate analysis spelled out in Python 1-2. Simple regression analysis (algorithm)
Run a Python file with relative import in PyCharm
Put Docker in Windows Home and run a simple web server with Python
Run a multi-line script in a PDB
Algorithm in Python (breadth-first search, bfs)
Maybe in a python (original title: Maybe in Python)
Write a binary search in Python
I made a simple typing game with tkinter in Python
[python] Manage functions in a list
First simple regression analysis in Python
Hit a command in Python (Windows)
Simple OAuth 2 in Python (urllib + oauthlib)
Sorting algorithm and implementation in Python
Let's run "python -m antigravity" in python
Create a DI Container in Python
Run shell command / python in R
It's hard to write a very simple algorithm in php
Draw a scatterplot matrix in python
ABC166 in Python A ~ C problem
Python algorithm
Develop an investment algorithm in Python 2
A simple way to avoid multiple for loops in Python
Create a binary file in Python
Algorithm in Python (depth-first search, dfs)
Solve ABC036 A ~ C in Python
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python