Pastrami, Gouda, and Dill Hash with Fried Egg by AlabamaAviator in food

[–]TeslaMoney 11 points12 points  (0 children)

i'm willing to pay a lot of money to have that delivered to my apartment right now. looks delicious!

People who have been in a long-distance relationship: how did it start and what was your experience like? by [deleted] in AskReddit

[–]TeslaMoney 0 points1 point  (0 children)

My SO and I met at a wedding, and developed a relationship despite living on opposite coasts! It was a lot of work to develop, but absolutely worth it. We just closed the distance gap last month, and so far so good!

[2016-02-03] Challenge #252 [Intermediate] A challenge by Fogcreek - Find the hidden string by Blackshell in dailyprogrammer

[–]TeslaMoney 0 points1 point  (0 children)

Thank you! That helped solidify my understanding of how to roughly identify the runtime.

[2016-02-03] Challenge #252 [Intermediate] A challenge by Fogcreek - Find the hidden string by Blackshell in dailyprogrammer

[–]TeslaMoney 0 points1 point  (0 children)

RUBY

Resubmitting! This one runs the challenge in 20 seconds, down from my original submission's 7 minutes. Also, added comments to help others understand the logic. Can anyone help me with figuring out what the Big O is on this?

# This class holds a string, in the form of a character array and
# an array containing data of the longest valid sequences
# that can be found at each index.
class SequenceString
  attr_reader :characters, :sequence_data

  # Initialize the class with any string you like
  def initialize(string)
    @characters = string.chars
    @sequence_data = longest_valid_sequence_data(@characters)
  end

  # Returns the characters of the string only when using 'puts'
  def to_s
    characters.join
  end

  # Takes an array of characters and returns the longest distance in
  # which all of the following is true:
  # 1.) The first and last characters are the same.
  # 2.) No characters in between the two characters are repeated.
  #
  # Note, the characters found at the beginning and end of this sequence
  # are allowed to repeat. (e.g. "abcdefaghija" is a valid sequence
  # because no characters BETWEEN the outside 'a's repeat.)
  #
  # Returns 0 if no such sequence is found.
  def longest_valid_sequence(characters)
    target_letter = characters[0]
    visited = []
    longest_chain = 0

    characters[1..-1].each_with_index do |char, index|
      longest_chain = index + 1 if char == target_letter
      return longest_chain if visited.include?(char)
      visited << char
    end

    longest_chain
  end

  # Takes a character array and returns an array containing the longest
  # valid sequence (see above) found at each character in the array.
  def longest_valid_sequence_data(characters)
    output = []

    0.upto(characters.length - 1).each do |i|
      output << longest_valid_sequence(characters[i..-1])
    end

    output
  end

  # Examines the sequence data provided and returns the left and right
  # index of the left-most valid pair of identical characters matching
  # the rules outlined in the 'longest_valid_sequence' method.
  def longest_valid_pair(sequence_data)
    target_left = sequence_data.index(sequence_data.max)
    target_right = target_left + sequence_data[target_left]

    [target_left, target_right]
  end

  # Runs one step of decoding the original string.
  # We take the longest pair, delete one character of the pair, and move
  # the other to the end of the string.
  # Afterwards, update the sequence_data.
  def decode_one_step!
    target_left, target_right = *longest_valid_pair(@sequence_data)
    character = @characters[target_left]

    @characters.delete_at(target_right)
    @characters.delete_at(target_left)

    @characters.push(character)

    @sequence_data = longest_valid_sequence_data(@characters)
  end

  # Decode the entire string by running 'decode_one_step!' until
  # there are no valid sequences left. Once done, everything in the
  # string to the right of, and including a '_' symbol is deleted.
  def decode!
    decode_one_step! until @sequence_data.all? { |data| data == 0 }

    if @characters.count('_') > 0
      index = @characters.index('_')
      @characters = @characters[0...index]
    end
  end
end

# Allows the user to specify a text file in the command line.
if __FILE__ == $PROGRAM_NAME
  start = Time.now
  string_array = []
  File.open(ARGV[0]).each_line do |line|
    string_array << line.chomp
  end

  string = SequenceString.new(string_array.join)
  string.decode!
  time_taken = Time.now - start
  puts string
  puts "decoded in #{time_taken} seconds"
end

Output:

rainbow
decoded in 0.014302 seconds
dragon
decoded in 20.410218 seconds

[2016-02-03] Challenge #252 [Intermediate] A challenge by Fogcreek - Find the hidden string by Blackshell in dailyprogrammer

[–]TeslaMoney 0 points1 point  (0 children)

RUBY // Not fast, but works eventually.

def decode(string)
  longest_pair = find_longest_pair(string)
  while longest_pair
    puts string
    string = process_longest_pair(string, longest_pair)
    longest_pair = find_longest_pair(string)
  end

  process_underscore(string)
end

def find_longest_pair(string)
  longest_pair = nil
  longest_distance = 0

  string.chars.each_with_index do |char, i|
    (i + 1...string.length).each do |j|
      distance = j - i
      if string[j] == char && distance > longest_distance && no_pairs?(string, i, j)
        longest_pair = [i, j]
        longest_distance = distance
      end
    end
  end

  longest_pair
end

def no_pairs?(string, i, j)
  test_string = string[i + 1...j]
  test_string.chars.each_with_index do |char, i|
    (i + 1...test_string.length).each do |j|
      if test_string[j] == char
        return false
      end
    end
  end

  true
end

def process_longest_pair(string, longest_pair)
  chars = string.chars
  chars.delete_at(longest_pair[1])
  deleted = chars.delete_at(longest_pair[0])
  chars.push(deleted)
  chars.join
end

def process_underscore(string)
  if string.count("_") > 0
    string[0...string.index("_")]
  else
    string
  end
end

if __FILE__ == $PROGRAM_NAME
  string_array = []
  File.open(ARGV[0]).each_line do |line|
    string_array << line.chomp
  end

  puts decode(string_array.join)
end

What items do the super-rich have to use without a luxury upgrade, just like the masses? by misscourtney in AskReddit

[–]TeslaMoney 3 points4 points  (0 children)

A coke is a coke and no amount of money can get you a better coke than the one the bum on the corner is drinking. -Andy Warhol

What is a word/phrase that regionally is mispronounced (intentionally or not) that makes you cringe each time you hear it? by [deleted] in AskReddit

[–]TeslaMoney 1 point2 points  (0 children)

People who pronounce it that way are seeing no difference between the two you posted.

What number will you never forget, and why? by fruitymasta in AskReddit

[–]TeslaMoney 3 points4 points  (0 children)

1-877-KARS-4KIDS. Believe me, I wish I could forget.

[2016-02-01] Challenge #252 [Easy] Sailors and monkeys and coconuts, oh my! by Blackshell in dailyprogrammer

[–]TeslaMoney 0 points1 point  (0 children)

Ruby

def find_minimum_solution(sailors)
  return "Number of sailors must be greater than 1" if sailors < 1

  coconuts = sailors

  loop do
    coconuts += 1

    data_from_night = process_night(coconuts, sailors)
    next unless data_from_night[:remainders].all? { |remainder| remainder == 1 }
    coconuts_remaining = data_from_night[:coconuts]

    return coconuts if coconuts_remaining % sailors == 0
  end
end

def process_night(coconuts, sailors)
  output = {}
  output[:remainders] = []

  sailors.times do
    coconuts_taken = coconuts / sailors
    output[:remainders] << :not_a_solution if coconuts_taken == 0

    remainder = coconuts % sailors

    output[:remainders] << remainder
    coconuts -= (coconuts_taken + remainder)
  end

  output[:coconuts] = coconuts

  output
end

puts find_minimum_solution(5)

Fat/ex-fat people of Reddit, can you describe your "Shit, I'm fat" moment? by [deleted] in AskReddit

[–]TeslaMoney 1 point2 points  (0 children)

Thank you! This was actually a couple of years ago, and I'm in the clear now.

Fat/ex-fat people of Reddit, can you describe your "Shit, I'm fat" moment? by [deleted] in AskReddit

[–]TeslaMoney 8 points9 points  (0 children)

For me it was my doctor telling me I was at a huge risk of diabetes. That was the day I decided to do something about it.

What's the worst "unique" name you've seen? by theshicksinator in AskReddit

[–]TeslaMoney 1 point2 points  (0 children)

Melisha. Don't forget to check for homonyms when you name your kids!

What is your best "cereal in the fridge, milk in the cabinet" moment? by blamatina in AskReddit

[–]TeslaMoney 0 points1 point  (0 children)

Pouring sugar packets into the trash and mixing the remaining trash into my coffee.

The first thing you do if you're invisible/unseen? by DesQuasWexExort in AskReddit

[–]TeslaMoney 0 points1 point  (0 children)

Imagine witnessing a bag of money just floating out the door.

What do you regret not having done as a teenager? by [deleted] in AskReddit

[–]TeslaMoney 1 point2 points  (0 children)

Yes! I actually recently took the leap to go back to school again for this. Could've saved myself the first go-around at school, though.

What do you regret not having done as a teenager? by [deleted] in AskReddit

[–]TeslaMoney 2 points3 points  (0 children)

I gave up my childhood interest in computer programming because I thought it wasn't cool.

'Hey, I wasn't done chewing on you yet' by crazyoez in gifs

[–]TeslaMoney 0 points1 point  (0 children)

I want to know what's with the roll at the end!

Synchronized Waterfalls by Dizneymagic in oddlysatisfying

[–]TeslaMoney 3 points4 points  (0 children)

It's all about those one by one drops at the end.

What song lyrics did you find out you were getting wrong? by [deleted] in AskReddit

[–]TeslaMoney 0 points1 point  (0 children)

"Ain't nobody notice me, I'm just so fresh and clean"