Skip to content

Instantly share code, notes, and snippets.

@ChanChar
ChanChar / total_from_string.rb
Created January 16, 2017 19:07
Parse and sums the integers within a sentence.
# Scan a given string for only numbers.
# Transform to int and add to a running total.
def total_from_string(str)
str.scan(/(\d+)/).flatten.reduce(0) { |total, value| total += value.to_i }
end
puts total_from_string("In 2015, I want to know how much does iPhone 6+ cost?") == 2021 # True
puts total_from_string("Also splits floats like 3.4 correctly") == 7 # True
puts total_from_string("Ignores integer signs such as -123 and +123") == 246 # True
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import os.path
import re
import sys
import tarfile
// write myMap(array, callback)
Assessment.myMap = function(array, callback) {
var mappedArr = [];
for (n=0; n < array.length; n++) {
mappedArr.push(callback(array[n]));
}
return mappedArr;
};
// write primes(n)
@ChanChar
ChanChar / matching_words.py
Created March 14, 2015 19:15
matching words solution
words = input().split()
repeats = []
for code_word in words:
if words.count(code_word) > 1 and code_word not in repeats:
repeats.append(code_word)
print(" ".join(sorted(repeats)))
@ChanChar
ChanChar / rps.py
Created February 21, 2015 19:13
Rock Paper Scissors Solution
number_of_cases = int(input())
for case in range(number_of_cases):
games = [outcome for outcome in input().split()]
player1_score = 0
player2_score = 0
player1_wins = ["PR", "RS", "SP"]
player2_wins = ["RP", "SR", "PS"]
@ChanChar
ChanChar / problem_skeleton.py
Created February 14, 2015 19:42
Codeabbey Skeleton
number_of_cases = int(input())
for case in range(number_of_cases):
new_case = input()
# new_case is a new line of data.
# CODE HERE
# Want to usually print(ANSWER, end=' ') which prints out an answer
# but also a space at the end so it's nicely formatted for submission.
@ChanChar
ChanChar / palindrome.py
Created February 14, 2015 19:30
2/14/2015 Solve Saturday Solutions
# Link: http://www.codeabbey.com/index/task_view/palindromes
# Outline:
# 1. Find the number of cases that will be in the problem.
# 2. For each case:
# 3. Either create a string or find a list of only letters
# 4. Remove case-sensitivity
# 5. Compare the string/list to its reversed version
# 6. Print "Y" if the comparisons are equal and "N" otherwise.
# 7. Run and check.
@ChanChar
ChanChar / answers.py
Created February 7, 2015 19:18
2/7/2015 Solve Saturday Q/A
# Modular Calculator
# I looked through a couple of other people's answers and it seems that most people had used a while loop.
# There is a much simpler solution using dictionaries/hash tables.
# http://www.codeabbey.com/index/task_view/modular-calculator
total = int(input()) # initial value
solved = False
while not solved:
operation, value = input().split()
if operation == "+":
total += int(value)
@ChanChar
ChanChar / gist:a178b5c164fb9e212c7d
Created January 31, 2015 19:33
1/31/2015 Problem of the Day Solution
# answer = A * B + C
# Return the sum of the digits of the answer
def sum_of_digits(number):
digits = [int(digit) for digit in list(str(number))] # Create a list of digits
return sum(digits) q # Then return the sum of the digits
number_of_cases = int(input())
for case in range(number_of_cases):
@ChanChar
ChanChar / primes.py
Created January 12, 2015 02:46
Next Prime Rmotr Assignment
### Assignment ###
#
# Your assignment is to implement the
# following function: `find_next_prime`.
# As the name states, given the number `n` the
# function should return the next closest prime.
#
# Examples:
# * `find_next_prime(6)` should return 7.
# * `find_next_prime(10)` should return 11.