Ruby and Python syntax ~ branch ~

Introduction

I read articles like "The guy who can only do Ruby is a fool who doesn't understand the algorithm" (I forgot where I read it) I often hear people say that they are not good at thinking about algorithms while hackathoning with pair pros.

The ** algorithm theory ** (sleepy) I took at university may be useful for the first time! I thought, so I would like to disassemble it in various ways.

First, I tried to organize the syntax that can be used.

Goal of this article

・ Conditional branching syntax can be used properly ・ Understand the difference between Ruby and Python code

What is the syntax in the first place?

How to write basic instructions for a program.

The program has only three movements.

-Sequential processing: Executed in order from the top. The basis is this movement.
-Branch processing: The processing is changed depending on the conditions. Movement like a psychological test.
-Repeat processing: Repeat the same processing. If you leave it alone, it will continue indefinitely, so combine it with some condition.

These writing rules are syntax!

By the way, variables, constants, assignments, etc. are sometimes referred to as syntax.

Conditional branch syntax

if The processing to be executed changes depending on the set conditions. You can also set the processing when the conditions are not met. Here's a diagram ↓ スクリーンショット 2020-04-15 21.34.58.png

You can also set multiple conditions. スクリーンショット 2020-04-15 19.20.31.png

Ruby if

Describe with ** if --elsif ・ ・ ・ else --end **.

if.rb


if a == b 
 puts "a and b are equal"
elsif a > b
 puts "a is large"
else
 puts "b is large"
end

Python if

Describe with ** if --elif ・ ・ ・ else **.

if.py


if a == b: 
 print("a and b are equal")
elif a > b:
 print("a is large")
else:
 print("b is large")

By the way

It seems that Implementing with if-elsif-else-end is faster than arranging multiple if-ends.

unless The process is executed when the set conditions are not met. The opposite of if. スクリーンショット 2020-04-15 21.35.03.png

Ruby unless

unless.rb


unless a = b 
 puts "a and b are not equal"
end

You can use else, but if you want to split the process into two, it is better to use ** if-else **. Use the negation operator (!) If you want the condition to be "when not".

unlessOrIf.rb


unless a == b 
 puts "a and b are not equal"
else
 puts "a and b are equal"
end

#Same as below
if a != b
 puts "a and b are not equal"
else
 puts "a and b are equal"
end

#After all this is fine
if a == b
 puts "a and b are equal"
else
 puts "a and b are not equal"
end

You cannot set multiple conditions with elsif. Use an if statement when it becomes a complicated branch.

Then do you use him unless?

  1. One process
  2. When the negative condition is easier to understand

Use unless for.

unless.rb


unless year > 20 
 puts "I can't buy alcohol"
end

Python unless

Not! !! However, you can write if not.

ifnot.rb


if not year > 20:
 print("I can't buy alcohol")

It seems that conditions can be set using the logical operators of and, or, not. For more information here

case (switch statement)

Case is recommended when you want to make multiple branches depending on some value. In the case of if, the conditional expression must be described for the number of branches, but in the case, only one condition is required. (Case is simpler to branch the menu depending on the input value of the Ruby curriculum "Review App"!) スクリーンショット 2020-04-15 21.41.37.png

Ruby case

case.rb


case a
when 1
 puts "a is 1"
when 2
 puts "a is 2"
else
 puts "Nothing"
end

The behavior is the same as when using == in the if condition and setting multiple branches.

You can also set two or more values to compare.

case.rb


case a
when 1,2
 puts "a is 1 or 2"
when 3,4
 puts "a is 3 or 4"
else
 puts "Nothing"
end

Python case

Not! !! !! !! According to the official documentation You can easily do the same by repeating> if ... elif ... elif ... else. There have been some suggestions for the syntax of switch statements, but there is (yet) no consensus on whether or how to make a range decision.

It seems that you write it with if-elif-else.

By the way

There is almost no difference in processing speed between if and case. The JS comparison article was also within the margin of error.

Summary

I was investigating this time and found the following (which may be obvious).

・ Syntax that can be used differs depending on the language
-The syntax to be used differs depending on the process you want to implement.

Basically, it is an if statement and only implements brute force, so I decided to use other syntax. However, there seems to be no basis for processing speed or memory usage, so Is it just a taste or readability? That is today's conclusion.

I wanted to write iteratively ...

Postscript

I want to write this time. ・ What is an algorithm in the first place? ・ How do you write a flowchart?

Recommended Posts

Ruby and Python syntax ~ branch ~
Python and Ruby split
Differences between Ruby and Python (basic syntax)
Python and ruby slice memo
Difference between Ruby and Python split
Scraping with Node, Ruby and Python
Differences between Ruby and Python in scope
Eating and comparing programming languages: Python and Ruby
Differences in syntax between Python and Java
with syntax (Python)
Encrypt with Ruby (Rails) and decrypt with Python
Easy web scraping with Python and Ruby
Python syntax-control syntax
SublimeText2 and SublimeLinter --Syntax check for Python3--
Instant method grammar for Python and Ruby (studying)
Specifying the range of ruby and python arrays
Get git branch name and tag name with python
About shallow and deep copies of Python / Ruby
Comparison of Python and Ruby (Environment / Grammar / Literal)
[python] Compress and decompress
Python and numpy tips
[Python] pip and wheel
Batch design and python
Python iterators and generators
Python packages and modules
Vue-Cli and Python integration
python input and output
Python control syntax (memories)
Python3, venv and Ansible
Python asyncio and ContextVar
AtCoder ARC080 D simulation solved in Ruby and Python
[Ruby vs Python] Benchmark comparison between Rails and Flask
Difference between Ruby and Python in terms of variables
Interprocess communication between Ruby and Python (POSIX message queue)
[Python] Chapter 05-01 Control syntax (comparison operator and conditional branching)
Comparison of CoffeeScript with JavaScript, Python and Ruby grammar
Version control of Node, Ruby and Python with anyenv
Solving with Ruby and Python AtCoder ARC 059 C Least Squares
Programming with Python and Tkinter
Python: Class and instance variables
Python 2 series and 3 series (Anaconda edition)
Python and hardware-Using RS232C with Python-
Mandelbrot Benchmark (C, PHP, HHVM, Ruby, Python, PyPy, and Kinx)
Python indentation and string format
Python real division (/) and integer division (//)
Solving with Ruby and Python AtCoder ABC178 D Dynamic programming
Install Python and Flask (Windows 10)
Java VS PHP VS Python VS Ruby
About python objects and classes
About Python variables and objects
Apache mod_auth_tkt and Python AuthTkt
Å (Ongustromu) and NFC @ Python
Solve with Ruby and Python AtCoder ABC133 D Cumulative sum
Understand Python packages and modules
# 2 [python3] Separation and comment out
[Python of Hikari-] Chapter 05-10 Control syntax (interruption and continuation of iteration)
Python shallow copy and deep copy
Python installation and basic grammar
Standard input / summary / python, ruby
I compared Java and Python!
Solving with Ruby and Python AtCoder AISING2020 D Iterative Squares