Skip to main content

Posts

Showing posts from August, 2017

Python

- Python 2 https://nbviewer.ipython.org/github/jmportilla/Complete-Python-Bootcamp/tree/master/ 3 / 2 = 1 3 / 2.0 = 1.5 float(3) / 2 = 1.5 Another method is using from __future__ import division >>> 3/2 1.5 Python 3 doesn't need to use import - Strings Python 3 from __future__ import print_function print('Hello World') vs Python 2 print 'Hello world' len('  ') checks string length  s = 'Hello World' # Grab everything past the first term all the way to the length of s which is len(s) s[1:] => "ello World' # Grab everything UP TO the 3rd index s[:3]=> 'Hel' #Everything s[:] => 'Hello World' # Grab everything, but go in step sizes of 2 s[::2] => 'HloWrd' # We can use this to print a string backwards s[::-1] =>  'dlroW olleH' - Python 2 More Strings s = 'STRING' 1. print 'Place another string with a mod and s: %s' %(s) Place anothe...

Ruby Formatting, ljust rjust, regex, .count

- Didn't realise that nil.to_i = 0 https://stackoverflow.com/questions/11029256/difference-between-nil-blank-and-empty - Interesting that .count and .size doesn't just count number of elements in array. If I did (1..5).count or .size # => 5 If I did .length, it would have an error. .count { | i | not i.to_s.include? '5' } can be used as a block. - .reject It's cool that reject blocks can be used in this manner: 1) .reject { |e| e.to_s.include?('5') } 2) .reject { |n| n.to_s =~ /5/ } Both achieve the same thing. - Ruby formatting. This is very new to me. ("%-5s" % "teg") => "teg  " It's like saying the format is 5 characters in a sring. https://idiosyncratic-ruby.com/49-what-the-format.html - .ljust expands the string or if there is an integer, fills the strings to the number of characters. "hello".ljust(4)            #=> "hello" "hello".ljust(20)           #=>...

Ternary Operators, Hash and Regex

- Doing name_shuffler.rb kata and competitive_eating scoreboard.rb - More on gsub Basically, anything in [xxx] will be replaced.  "hello".gsub(/[aeiou]/, '*')                  #=> "h*ll*" the \1 means the group number. Has to come with a ([xxx]). Can be \any_number "hello".gsub(/([aeiou])/, '<\1>')             #=> "h<e>ll<o>" Using a block, sub everything  "hello".gsub(/./) {|s| s.ord.to_s + ' '}      #=> "104 101 108 108 111 " This is the use of a group name. It says to replace everything with the group name with similar words from the group. ?<groupname>[xxx] and \k<groupname>. "hello".gsub(/(?<foo>[aeiou])/, '{\k<foo>}')  #=> "h{e}ll{o}" This is to replace specifically.  'hello'.gsub(/[eo]/, 'e' => 3, 'o' => '*')    #=> "h3ll*" - T...

.map, .fetch, .ord, word indexes, .cycle

- Did encode.rb and duplcate_encoder - .fetch in ruby a = [ 11, 22, 33, 44 ] a.fetch(1)               #=> 22 a.fetch(-1)              #=> 44 a.fetch(4, 'cat')        #=> "cat" a.fetch(4) { |i| i*i }   #=> 16 - .map is the same as .collect - ['a', 'b'].map{|x| '1'} basically replaces the a and b with 1, still in an array - How to combine zip and map https://stackoverflow.com/questions/5609681/ruby-adding-subtracting-elements-from-one-array-with-another-array You could use zip: a = [1,2,3,4] b = [2,3,4,5] a.zip(b).map { |x, y| y - x } # => [1, 1, 1, 1] There is also a Matrix class: require "matrix" a = Matrix[[1, 2, 3, 4]] b = Matrix[[2, 3, 4, 5]] c = b - a # => Matrix[[1, 1, 1, 1]] - .codepoints/ .ord vs bytes https://stackoverflow.com/questions/40849265/bytes-vs-codepoints-in-ruby bytes returns individual bytes, regardless of char size, whereas...