Skip to content

Instantly share code, notes, and snippets.

@shidel-dev
Last active August 29, 2015 13:56
Show Gist options
  • Save shidel-dev/9164949 to your computer and use it in GitHub Desktop.
Save shidel-dev/9164949 to your computer and use it in GitHub Desktop.
#=====bob
class Bob
hey:(words) ->
switch
when words.replace(/\s+/, '') == '' then "Fine. Be that way!"
when words == words.toUpperCase() then "Woah, chill out!"
when /\?$/.test(words) then "Sure."
else "Whatever."
module.exports = Bob
#=====word count
class Words
constructor: (@sentence) ->
totals = {}
for word in @sentence.replace(/[^\w\s]+/g,'').toLowerCase().split(/\s/)
if word != ''
totals[word] ?= 0
totals[word] += 1
@count = totals
module.exports = Words
#===binary string to decimal
class Binary
constructor: (@str) ->
toDecimal: ->
nums = (parseInt(num) for num in @str.split('').reverse() when num in ['0', '1'])
decimal = 0
for i in [0...nums.length]
decimal +=(nums[i] * (2**i))
decimal
module.exports = Binary
#===detecting anagrams given an a dictanary and a reference word
class Anagram
constructor: (word) ->
@ref = word.toLowerCase()
match: (set) ->
(word.toLowerCase() for word in set\
when word.toLowerCase() != @ref and this.organize(@ref) == this.organize(word))
organize: (word)->
word.toLowerCase().split('').sort().join()
module.exports = Anagram
#===trinary string to decimal
class Trinary
constructor: (@str) ->
toDecimal: ->
nums = (parseInt(num) for num in @str.split('').reverse() when num in ['0', '1','2'])
decimal = 0
for i in [0...nums.length]
decimal +=(nums[i] * (3**i))
decimal
module.exports = Trinary
#====hex string to decimal
class Hexadecimal
constructor: (@str) ->
hexDigits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
toDecimal: ->
nums = (num for num in @str.split('').reverse() when num in hexDigits)
if nums.length == @str.length
decimal = 0
for i in [0...nums.length]
decimal += (hexDigits.indexOf(nums[i]) * (16**i))
decimal
else
return 0
module.exports = Hexadecimal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment