Skip to content

Instantly share code, notes, and snippets.

[tool.poetry]
name = "tmpdir2"
version = "0.1.0"
description = ""
authors = ["Adam Smith <my@email.com>"]
[tool.poetry.dependencies]
python = "^3.7"
[tool.poetry.dev-dependencies]
@NotTheEconomist
NotTheEconomist / rps.py
Last active November 26, 2018 09:15
Rock Paper Scisccors with deques
import collections
def rps(*choices):
"""choices must be in order so that each proceeding element defeats its neighbor.
Returns a function that takes two members of choices and decides which one would win in that RPS game.
rps("rock", "paper", "scissors") or rps("paper", "scissors", "rock"), but never rps("scissors", "paper", "rock")
"""
def combine_dicts(a, b, *, op=lambda x: x, default=None):
result = {}
for k, va in a.items():
vb = b.get(k, default)
result[k] = op(va, vb)
return result
everyOther :: [a] -> [a]
everyOther xs = [v | (k,v) <- zip (cycle [True, False]) xs, k]
betweenAF :: String -> Bool
betweenAF = all (flip elem ['a'..'f']) . everyOther
import Data.Char(isSpace)
-- splitOnTest (=='x') "AxBxC" -> ["A", "B", "C"]
splitOnTest :: (Char -> Bool) -> String -> [String]
splitOnTest = splitOnTest' []
where splitOnTest' :: String -> (Char -> Bool) -> String -> [String]
splitOnTest' acc _ [] = [(reverse acc)]
splitOnTest' acc f (x:xs) | f x = [(reverse acc)] ++ splitOnTest f xs
| otherwise = splitOnTest' (x:acc) f xs
import Data.List.Split(splitOn)
import qualified Data.Text as T
parse :: String -> [Int]
parse s | '-' `elem` s = expandedS
| otherwise = singleS
where expandedS = let (x':(y':[])) = splitOn "-" s
x = read x' :: Int
y = read y' :: Int
in [x..y]
Name of the file: test.txt
vowels: 20
consonants: 39
punctuation: 15
import zlib
import base64
import json
import sys
def flip_entity(entity):
# translation tables
newX = [-3,-2, 0, 2,3,2,0,-2]
import Data.Char(toUpper, toLower)
alternateCase :: String -> String
alternateCase [] = []
alternateCase (x:[]) = [toUpper x]
alternateCase (x:y:xs) = (toUpper x):(toLower y):(alternate xs)
alternateCase "abcdefg"
-- "AbCdEfG"
import Control.Monad
problem1 :: IO ()
problem1 = do
input <- replicateM 3 getLine
putStrLn $ show . sum . map (read :: String -> Int) $ input
problem2 :: IO ()
problem2 = do
input <- replicateM 2 getLine