A lot of us have struggled with algorithms and data structures. When I began with programming at my University, the name of a subject that got my attention and got me motivated to find my life call was the Principles of programming. I started devoting a lot of time to figuring out how to solve professors' assignments. Now those algorithms are easy for me but then they were not, and I couldn’t pass the exam, I had the wrong approach in learning, you can not memorize them, you must practice and figure out what you need to do. We draw flowcharts to show results using just if and while loop, without any predefined code. I think now that that was the best subject where we could learn how to create an algorithm, how to think like programmers. My reason for writing this article is to give you basic problems that occur the most in everyday programming.1. Create function pozpar that for argument have an array of whole numbers y. Function needs to return every element of the array that is positive and even, reduced by 2 and the rest of the elements to enlarge by 1
def pozpar(y)
n = y.length
i = 0
while i < n
e = y[i].round
q = e / 2
if e > 0 && q * 2 == e
y[i] = e - 2
else
y[i] = e + 1
end
i+=1
end
return y
end
p pozpar([1,2,3,4.4,5,6])
Output: [2, 0, 4, 2, 6, 4]
Pretty straight forward, in variable n I placed a length of an array, then set variable i to be 0 while i is smaller than the length of array loop through elements, floats are not allowed, convert them to the nearest integer, then make a condition to ensure that numbers must be positive and even, change elements of an array, even numbers smaller by 2 and odd numbers bigger by 1. This assignment is done.2. Write a function with two arguments, both of them strings s1 and s2, if in string s2 big letter exists substitute it with the first element of string s1. The output should be string s2 only if substitution happened, if not output friendly message.
def strings(s1,s2)
i=0
n2 = s2.length
count = 0
while i < n2
if s2[i] >= 'A' && s2[i] <= 'Z'
s2[i] = s1[0]
count +=1
end
i+=1
end
if count > 0
p s2
else
print "There is no change in s2"
end
end
strings('zDkalskdlAKk123@', 'AlldapPWDA<zx4567?')
Output: "zlldapzzzz<zx4567?"
So, I set i to be 0, n2 is the length of string s2, I started iterating through a string, Uppercase A has ASCII value 65 in decimal, for Z, the value is 90 in decimal, in if statement I am looking for big letters if there is any I will substitute it with the first element of s1, count that needs to check will there be any iterations if not output will be a friendly message. Back then, as a student, my first year, the first exam, when I saw the assignment, as this one is, I would feel like the most stupid person in the world. If I practiced more everything would be ok.3. Create a function that accepts string t. If the string doesn’t contain a number, output number of big letters in the string, if it does contain number reduce those numbers bigger of 3 by 1. Output changed string.
def stringy(t)
count = 0
i = 0
n = t.length
res = false
while i < n
if !(t[i] >= '0' && t[i] <= '9')
if t[i] >= 'A' && t[i] <= 'Z'
count+=1
res = true
end
elsif t[i] >= '0' && t[i] <= '9'
if t[i] > '3'
inte = t[i].to_i
t[i] = (inte -1).to_s
end
res = false
end
i+=1
end
if res == true
p count
else
p t
end
end
stringy('asdjJKLj1256657')
Output: ‘asdjJKLj1245546’
Read and do, if it doesn’t contain number count letters if it does reduce those bigger than 3 by 1. I added variable res to be false so I And the last assignment on the exam for future Ruby developer.4. Create a function with string r, which needs to check if a string is a name, for a string to be a name it must contain the first big letter, and the rest of the letters should be small. Output friendly message to tell the user if a string can be a name or not.
def name(s)
i = 0
j = 1
n = s.length
res = false
while (j < n)
if s[i] >= 'A' && s[i] <= 'Z' && s[j] >= 'a' && s[j] <= 'z'
res = true
else
res = false
end
j+=1
end
if res == true
print "This string can be a name"
else
print "This string can't be a name"
end
end
name('Ivana')
Ouptut: This string can be a name
Very easy, I set variable i on 0, variable j on 1, n contain the length of string, while j < n, check if the first element is a big letter and check are the rest of the letters small, number or any other character is not allowed, so where I want something to be true I used variable res, everything different from the desired result would result in false.Trying to do algorithms without making it easier, trying to do it without using a lot of predefined functions, with just two loops, give us a better understanding of how something works, without using predefined functions we can understand easier what they are doing when we start using them. To check big letters I could just do str.uppercase and get a job done but it won't be a problem to see ASCII table and try to do it without that function. Without using each, each_with_index, unless, for, without regular expression, map, count, etc, we can do it without those helpers that make our life easier. A good practice is to code all of these loops and functions like I did those algorithms, using just a while, and if loops, with practice like this we can understand easier how those predefined functions work. And with practice like this, we will not have problems on interviews with small problems, like the part where we need to do something with the string, or array, we won’t fail because of some stupid mistake where we didn’t pay attention, fail will be easier, for next interview we will be prepared, not disappointed because of beginner mistake.