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.
・ Conditional branching syntax can be used properly ・ Understand the difference between Ruby and Python code
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.
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 ↓
You can also set multiple conditions.
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
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")
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.
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.
Use unless for.
unless.rb
unless year > 20
puts "I can't buy alcohol"
end
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 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"!)
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
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.
There is almost no difference in processing speed between if and case. The JS comparison article was also within the margin of error.
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 ...
I want to write this time. ・ What is an algorithm in the first place? ・ How do you write a flowchart?
Recommended Posts