End of the foundations

We started today by solving some Leetcode problems. Not super challenging, if we do not take Big Oh notation into consideration. One example is Fibonacci sequence, normally I will just solve it with recursion, say fib(n) = r fib(n-1) + fib(n-2), but if we take time complexity into consideration, recursion is really not a good idea. There are better ways to deal with it, but that would need some knowledge about Matrix and stuff. I will definitely review some of my linear algebra knowledge and try to get on hold of the fib thing.

Fibonacci can be written into something like this, but this is nothing close to computer science, this is totally math.

Will try to figure out a way to deal with the tribonacci thing in the Leetcode with some Math approach. (TO_DO_LIST)

Today our project is pretty interesting, which is to create a to do list, which is very similar to Angela Yu’s course on Udemy, but more in a DIY style. Some of the approaches that recommended by the instructions are actually amazing especially when you want to expend the app. I guess that’s also the amazing thing of OOP, which allows you to expand the program easily, instead of rewriting a lot of methods.

First Part is the Item.

I shared my understanding of Regex with my coding pair.

The most accurate version would be the one that we used down there, this regex approach can save a lot of time.

For now my understanding is:

  1. “^” means the start of the string that we want to match
Regex = \^(19|20)\d{2}-(0[1-9]|1[02])-(0[1-9]|[12]\d|[3][01])$\def check_regex(regex, string)    string =~ regexendcheck_regex(Regex, "2020-02-02") #will return 0 but still a truthy value in ruby.
check_regex(Regex, "2020-15-15") #will return nil -- a falsy value.
# LIST.RB
require './items.rb'class List
#review delete items later
attr_accessor :label
attr_reader :items
def initialize(label)
@label = label
@items = []
end
def toggle_item(index)
index = index.to_i
items[index].toggle
end
def add_item(title, deadline, description = "")
item = Item.new(title, deadline, description)
return false if !Item.valid_date?(deadline)
@items << item
return true
end
def size
return items.length
end
def valid_index?(index)
index = index.to_i
index <= size && index >= 0
end
def swap(index_1, index_2)
index_1 = index_1.to_i
index_2 = index_2.to_i
return false if !valid_index?(index_1) || !valid_index?(index_2)
@items[index_1], @items[index_2] = @items[index_2], @items[index_1]
return true
end
def [](index)
index = index.to_i
return nil if !valid_index?(index)
@items[index]
end
def priority
@items[0]
end
def print
l = @label.upcase
puts "------------------------------------------"
puts " #{l}"
puts "------------------------------------------"
puts "Index | Item | Deadline | done"
@items.each_with_index do |item, idx|
checkmark = (item.done ? "[✓]" : "[ ]")
puts "#{idx} | #{item.title} | #{item.deadline} | #{checkmark}"
end
end
def print_full_item(index)
if valid_index?(index)
checkmark = (item.done ? "[✓]" : "[ ]")
puts "------------------------------------------"
puts "#{@items[index].title} #{@items[index].deadline} | #{checkmark}"
puts "#{@items[index].description}"
puts "------------------------------------------"
else
puts "------------------------------------------"
puts "Item doesn't exist"
puts "------------------------------------------"
end
end
def print_priority
print_full_item(0)
end
def up(index, amount = 1)
index = index.to_i
amount = amount.to_i
return false if !valid_index?(index)
# array = [1,2,3,4,5]
# index = 4 amount = 2
# expect = [1,4,2,3,5]
while amount > 0
swap(index, index - 1)
index -= 1
break if index == 0
amount -= 1
end
return true
end
def down(index, amount = 1)
index = index.to_i
amount = amount.to_i
return false if !valid_index?(index)
# debugger
while amount > 0
swap(index, index + 1)
index += 1
break if index == self.size - 1
amount -= 1
end
return true
end
def sort_by_date!
@items.sort_by! {|item| item.deadline}
end
def remove_item(index)
index = index.to_i
if valid_index?(index)
@items.delete_at(index)
return true
else
return false
end
end
def purge
@items.delete_if {|item| item.done}
end
end

Codes might seems to be complicated, but logic is really simple. We built it from scratch.

Learned:

  1. If we want to take input from user, say num = gets.chomp, we will keep in mind one thing: convert the type!!! All the stuff we get from the gets() are strings. If we need integer, we will have to to_i it before using it. These problems caused a lot of bug. More than I could imagine.
user_input = gets.chompcase user_input
when "hi"
print "hello world"
when "no"
print "no what?"
when "yes"
print "yes sir"
else
print "bye-bye"
end

This is a basic case when block. Pretty easy to understand.

3. a, b, *c = gets.chomp.split(“ ”)

this is an interesting way of getting info from the user. Meaning that we are actually expecting multiple input from the user. The 3rd element could be any number of anything. But, it’s not an array. If I understand it right. The third element *c is now a equivalent of Clang’s pointer. It’s the starting point of multiple elements. Could be strings, arrays, ints or whatever. Now it doesn’t have specific type but became a pointer.

4. position = [1,2]; a, b = position. this is doable in Ruby. Ruby’s syntax sugar is really sweet, but sometimes too sweet and let me forget that in the real world I should be more careful with the grammars. Now if you want me to write in C, my code will be very sloppy.

TO_DO_LIST

  1. Get familiar with regex

Designer transforming into a developer