DEV Community

Brittany
Brittany

Posted on • Updated on

Day 18 of #100daysofCode - 3 challenges

Codewar Challenges

Today I worked on three coding challenges, while I KNOW there are easier ways to do all of these, I was happy to find a solution nonetheless. Feel free to comment on the best ways to accomplish these challenges.

Challenge 1

Write a function that accepts an array of 10 integers (between 0 and 9), that returns a string of those numbers in the form of a phone number.

For example,

createPhoneNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
 # => returns "(123) 456-7890"
Enter fullscreen mode Exit fullscreen mode

My Solution

def createPhoneNumber(numbers)
  one = numbers[0]
  two = numbers[1]
  three = numbers[2]
  four = numbers[3]
  five = numbers[4]
  six = numbers[5]
  seven = numbers[6]
  eight = numbers[7]
  nine = numbers[8]
  ten = numbers[9]

  "(#{one}#{two}#{three}) #{four}#{five}#{six}-#{seven}#{eight}#{nine}#{ten}"

end
Enter fullscreen mode Exit fullscreen mode

Challenge 2

An isogram is a word that has no repeating letters, consecutive or non-consecutive. Implement a function that determines whether a string that contains only letters is an isogram. Assume the empty string is an isogram. Ignore letter case.

For example,

is_isogram("Dermatoglyphics" ) == true
is_isogram("aba" ) == false
is_isogram("moOse" ) == false # -- ignore letter case
Enter fullscreen mode Exit fullscreen mode

My Solution

def is_isogram(string)
s = string.downcase
("a".."z").all?{|c| s.count(c) <= 1}

end
Enter fullscreen mode Exit fullscreen mode

Challenge 3

You probably know the "like" system from Facebook and other pages. People can "like" blog posts, pictures or other items. We want to create the text that should be displayed next to such an item. Implement a function likes :: [String] -> String, which must take in input array, containing the names of people who like an item. It must return the display text as shown in the examples:

likes [] // must be "no one likes this"
likes ["Peter"] // must be "Peter likes this"
likes ["Jacob", "Alex"] // must be "Jacob and Alex like this"
likes ["Max", "John", "Mark"] // must be "Max, John and Mark like this"
likes ["Alex", "Jacob", "Mark", "Max"] // must be "Alex, Jacob and 2 others like this"
Enter fullscreen mode Exit fullscreen mode

My Solution

def likes(names)
  #your code here
  if names.empty? 
    return "no one likes this" 
  elsif names.length == 1
    return names[0] + " likes this"
  elsif names.length == 2
   return names[0] + " and " + names[1] + " like this"
  elsif names.length == 3
   return names[0] + ", " + names[1] + " and " + names[2] + " like this"
  else names.length > 3
    total = names.length - 2
    return names[0] + ", " + names[1] + " and " + total.to_s + " others like this"
  end
end
Enter fullscreen mode Exit fullscreen mode

As I learn more, I plan to keep doing coding challenges. I will post more as I do them and hopefully we can learn from each other.

Top comments (0)