Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Wednesday, May 3, 2017

State of the Mission, 2017-05-03: Lots of Coding

I know I've been very quiet lately, but there's a good reason for it.

Here's what I've been doing on Verbalyser:










And this is what I've been focused on for the last few days.  I'm not at liberty to share my code, but I think it's pretty clear how I've gone about it.




 



Tuesday, February 7, 2017

Rails 4 in Action, Chapter One (Notes)


Not much to say, just wrapping my head around the basic functionality afforded by the scaffold in the examples of Chapter One.






Wednesday, December 14, 2016

T-Shirt: I Don't Always Test My Code...

I have T-shirts on Redbubble, some of them geeky... ok, ok, they're all geeky.

Anyway, here's the latest one, go check out the rest.


Thursday, December 8, 2016

Arrays of Animals

From Exercise 34 of Learn Ruby the Hard Way.  I am sure this could be done a lot more elegantly, but I'm satisfied with what I got from this, and I know it works too.
# everything pertaining to the quiz is contained in the function below

def quiz

  # clear the screen
  puts "\e[H\e[2J"

  # gem for colorising text, very cool stuff
  require 'colorize'

  # the array of animals
  animals = ['bear', 'ruby', 'peacock', 'kangaroo', 'whale', 'platypus']

  # display the list of animals, it's not a memory test
  puts "These are the animals in our array: #{animals}"

  # preparing the ordinal and cardinal arrays for the quiz
  ordinal = ["first", "second", "third", "fourth", "fifth", "sixth"]
  cardinal = [0, 1, 2, 3, 4, 5]

  # generic question for choosing the ordinal position
  chooseOrdinal = """
  What is the ordinal position of this animal?
  first
  second
  third
  fourth
  fifth
  sixth


  """

  # generic question for choosing the cardinal position
  chooseCardinal = """
  What is the cardinal position of this animal?
  0
  1
  2
  3
  4
  5


  """

  puts """
  Type the name of an animal:
  - bear
  - ruby
  - peacock
  - kangaroo
  - whale
  - platypus

  """
  print "> "

  userSelection = $stdin.gets.chomp.downcase
  case userSelection
  when "#{animals[0]}"
    puts "You chose a #{userSelection}.\n"
    puts chooseOrdinal
    answerOrdinal = $stdin.gets.chomp.downcase
    if answerOrdinal == "#{ordinal[0]}"
      puts "Correct!\n".colorize(:green)
      puts chooseCardinal
    print "> "
      answerCardinal = $stdin.gets.chomp
      if answerCardinal == "#{cardinal[0]}"
        puts "Correct!\n".colorize(:green)
      else
        puts "Incorrect cardinal number!".colorize(:red)
      end
    else
      puts "Incorrect ordinal number!".colorize(:red)
    end
  when "#{animals[1]}"
    puts "You chose a #{userSelection}."
    puts chooseOrdinal
    answerOrdinal = $stdin.gets.chomp.downcase
    if answerOrdinal == "#{ordinal[1]}"
      puts "Correct!\n".colorize(:green)
      puts chooseCardinal
    print "> "
      answerCardinal = $stdin.gets.chomp
      if answerCardinal == "#{cardinal[1]}"
        puts "Correct!".colorize(:green)
      else
        puts "Incorrect cardinal number!".colorize(:red)
      end
    else
      puts "Incorrect ordinal number!"
    end
  when "#{animals[2]}"
    puts "You chose a #{userSelection}."
    puts chooseOrdinal
    answerOrdinal = $stdin.gets.chomp.downcase
    if answerOrdinal == "#{ordinal[2]}"
      puts "Correct!\n".colorize(:green)
      puts chooseCardinal
    print "> "
      answerCardinal = $stdin.gets.chomp
      if answerCardinal == "#{cardinal[2]}"
        puts "Correct!".colorize(:green)
      else
        puts "Incorrect cardinal number!".colorize(:red)
      end
    else
      puts "Incorrect ordinal number!"
    end
  when "#{animals[3]}"
    puts "You chose a #{userSelection}."
    puts chooseOrdinal
    answerOrdinal = $stdin.gets.chomp.downcase
    if answerOrdinal == "#{ordinal[3]}"
      puts "Correct!\n".colorize(:green)
      puts chooseCardinal
    print "> "
      answerCardinal = $stdin.gets.chomp
      if answerCardinal == "#{cardinal[3]}"
        puts "Correct!".colorize(:green)
      else
        puts "Incorrect cardinal number!".colorize(:red)
      end
    else
      puts "Incorrect ordinal number!"
    end
  when "#{animals[4]}"
    puts "You chose a #{userSelection}."
    puts chooseOrdinal
    answerOrdinal = $stdin.gets.chomp.downcase
    if answerOrdinal == "#{ordinal[4]}"
      puts "Correct!\n".colorize(:green)
      puts chooseCardinal
    print "> "
      answerCardinal = $stdin.gets.chomp
      if answerCardinal == "#{cardinal[4]}"
        puts "Correct!".colorize(:green)
      else
        puts "Incorrect cardinal number!".colorize(:red)
      end
    else
      puts "Incorrect ordinal number!"
    end
  when "#{animals[5]}"
    puts "You chose a #{userSelection}."
    puts chooseOrdinal
    answerOrdinal = $stdin.gets.chomp.downcase
    if answerOrdinal == "#{ordinal[5]}"
      puts "Correct!\n".colorize(:green)
      puts chooseCardinal
    print "> "
      answerCardinal = $stdin.gets.chomp
      if answerCardinal == "#{cardinal[5]}"
        puts "Correct!".colorize(:green)
      else
        puts "Incorrect cardinal number!".colorize(:red)
      end
    else
      puts "Incorrect ordinal number!".colorize(:red)
    end
  else
    puts "I know no such animal."
  end

end

quiz()

Repairing Shoddy Code

Original, Shoddy Code


module Ex2
  # This function will break up words for us.
  def Ex25.brak_words(stuff
    words = stuff.split(' ')
    return word
  end

  # Sorts the words.
  def Ex25.sortwords(words)
    return words.sort
  end

  # Prints the first word after popping it off.
  df Ex25.print_first_word(words)
    word = words.pop(1)
    puts wor
  end

  # Prints the last word after popping it off.
  def Ex25:print_last_word(words)
    word = words.pop
    put word
  end

  # Takes in a full sentence and returns the sorted words.
  def Ex25.sort_sentence(sentence)
    words = Ex25.break_words(sentence)
    return Ex25.sort_words(words)
  ed

  # Prints the first and last words of the sentence.
  def Ex25.print_first_and_last(sentence
    words = Ex25.break_words(sentenc)
    Ex25.print_first_wrd(word)
    Ex25.print_last_word(words)
  end

  # Sorts the words then prints the first and last one.
  def Ex25.print_first_and_last_sorted(sentence)
    words = Ex25.sort_sentence(sentence)
    Ex25.print_fist_word(words)
    Ex25.print_last_word(words)
  end



puts "Let's practice everything."
puts 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.'

poem = &lt&ltEND
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none.
ENDED

puts "--------------"
puts poem
puts "--------------"


five = 10 - 2  3 - 6
puts "This should be five: #{five"

def secret_formula(started)
  jelly_bens = started * 500
  jars = jelly_beans / 1000
  crate = jars / 100
  return jelly_beans, jars, crates
end


start_point = 10000
beans, jars crates = secret_formula(start_point)

puts "With a starting point of: #{start_point}"
puts "We'd have #{beans beans, #{jars} jars, and #{crates} crates."

start_point = start_point / 10

sentence = "All good things come to those who wait."
words = Ex25.break_words(sentence)
sorted_words = Ex25.sort_words(words)
Ex25.print_first_word(wrds)
Ex25.print_last_word words)
Ex25.print_first_word(sort_words)
Ex25.print_last_word(sorted_words)
sorted_words = Ex25.sort_sentenc(sentence)
Ex25.print_first_and_last(sentence)
Ex25:print_first_and_last_sorted(sentence)

Fresh, Clean Code

# Import the module Ex25 and define the functions

module Ex25

  # This function will break up words for us.
  def Ex25.break_words(stuff)
    words = stuff.split(' ')
    return words
  end

  # Sorts the words.
  def Ex25.sort_words(words)
    return words.sort
  end

  # Prints the first word after popping it off.
  def Ex25.print_first_word(words)
    word = words.shift
    puts word
  end

  # Prints the last word after popping it off.
  def Ex25.print_last_word(words)
    word = words.pop
    puts word
  end

  # Takes in a full sentence and returns the sorted words.
  def Ex25.sort_sentence(sentence)
    words = Ex25.break_words(sentence)
    return Ex25.sort_words(words)
  end

  # Prints the first and last words of the sentence.
  def Ex25.print_first_and_last(sentence)
    words = Ex25.break_words(sentence)
    Ex25.print_first_word(words)
    Ex25.print_last_word(words)
  end

  # Sorts the words then prints the first and last one.
  def Ex25.print_first_and_last_sorted(sentence)
    words = Ex25.sort_sentence(sentence)
    Ex25.print_first_word(words)
    Ex25.print_last_word(words)
  end
end

# Execute the functions defined from module Ex25

sentence = "All good things come to those who wait."
words = Ex25.break_words(sentence)
sorted_words = Ex25.sort_words(words)
Ex25.print_first_word(words)
Ex25.print_last_word(words)
Ex25.print_first_word(sorted_words)
Ex25.print_last_word(sorted_words)
sorted_words = Ex25.sort_sentence(sentence)
Ex25.print_first_and_last(sentence)
Ex25.print_first_and_last_sorted(sentence)

puts "--------------"

# Seperate code, nothing to do with module Ex25

puts "Let's practice everything."
puts 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.'

poem = &lt&ltEND
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none.
END

puts "--------------"
puts poem
puts "--------------"

# Separate code again, nothing to do with module 25 or the above section

five = 10 - 2 + 3 - 6
puts "This should be five: #{five}"

puts "--------------"


def secret_formula(started)
  jelly_beans = started * 500
  jars = jelly_beans / 1000
  crate = jars / 100
  return jelly_beans, jars, crate
end

start_point = 10000
beans, jars, crates = secret_formula(start_point)

puts "With a starting point of: #{start_point}"
puts "We'd have #{beans} beans, #{jars} jars, and #{crates} crates."

puts "--------------"

start_point = start_point / 10

puts "Now, with a starting point of: #{start_point}"
puts "We'd have #{beans} beans, #{jars} jars, and #{crates} crates."

Monday, October 24, 2016

Thoughts on Refactoring Code

http://www.ben-morris.com/wp-content/library/refactoring-vs-rewriting.gif

I'm working on my CareerCoach job research tool, and I'm finding it very tricky to refactor the code.
There are too many moving parts, too many variables.

I'm trying to convert blocks of code into functions, which means that I have to turn variables into global variables, and that gets confusing.

I think I need to map out in more detail what I'm trying to achieve and then work on each component separately.

For now, it's time for a break.

1,050 hours

It took me 13 working days to complete my first 100 "work" pomodoros as a Junior Software Tester at Profectus Group.  Much of ...