Skip to content

Instantly share code, notes, and snippets.

View vituscze's full-sized avatar

Vít Šefl vituscze

View GitHub Profile
using System.Text.RegularExpressions;
/*
For this exercise, a word is any sequence of uppercase or lowercase characters from the Latin alphabet
(A - Z, a - z). A sentence is a sequence of characters that ends with a period (.) or question mark (?).
Write a program that reads a file input.txt and writes out all words that occur in at least two
sentences. Ignore case: "Alice" and "alice" are the same word. Write the words in alphabetical order,
one per line, all in lowercase. For each word, indicate how many sentences it occurs in.
Your program should be reasonably efficient. (Specifically, if the input contains N distinct words, your
program's expected running time should be less than O(N^2).)
import Data.List
is_equiv :: (a -> a -> Bool) -> [a] -> Bool
is_equiv (~=) set = refl && sym && trans
where
infix 1 ==>
x ==> y = not x || y
every c = all c set
import Data.Char
import Data.List
search :: [String] -> [String] -> [Int]
search grid = map (count . map toLower)
where
gridA = map (map toLower) grid
gridB = transpose gridA
line s = length . filter (s `isPrefixOf`) . tails
factors1 :: Integer -> Integer -> [Integer]
factors1 k n
| k * k > n = [n]
| n `mod` k == 0 = k : factors1 k (n `div` k)
| otherwise = factors1 (k + 1) n
factors :: Integer -> [Integer]
factors 1 = []
factors n = factors1 2 n
pos(P) :- member(P, [a,b,c,d]).
next(go(P), [M, Box1, Box2, B], [P, Box1, Box2, B]) :- pos(M), pos(P).
next(stack, [M, M, M, B], [M, box2, M, B]).
next(unstack, [M, box2, M, B], [M, M, M, B]).
next(push(box1, P), [M, M, Box2, B], [P, P, Box2, B]) :- pos(P).
next(push(box2, P), [M, Box1, M, B], [P, Box1, P, B]) :- pos(P).
next(climb_on_1, [M, Box1, Box2, B], [box1, Box1, Box2, B]) :- M = Box1; Box1 = box2, M = Box2.
next(climb_off_1, [box1, Box1, Box2, B], [M, Box1, Box2, B]) :- pos(Box1), M = Box1; Box1 = box2, M = Box2.
next(grab, [box1, box2, B, B], [box1, box2, B, hand]).
match_one(one(L), LIn, LOut) :- append(L, LOut, LIn).
match_one(star(L), LIn, LOut) :- LIn = LOut; match_one(plus(L), LIn, LOut).
match_one(plus(L), LIn, LOut) :- match_one(one(L), LIn, LMid), match_one(star(L), LMid, LOut).
match_one(opt(L), LIn, LOut) :- LIn = LOut; match_one(one(L), LIn, LOut).
match(Expr, Str) :- foldl(match_one, Expr, Str, []).
decode_char(Key, A, B) :- B #= (A - 97 + Key) mod 26 + 97.
decode_string(_, _, [], []).
cone([o,o,o], 0).
cone(R, 1) :- select(x, R, [o,o]).
cone(R, 2) :- select(o, R, [x,x]).
cone([x,x,x], 3).
match([_,_], []).
match([A,B,C|R], [M|RM]) :- cone([A,B,C],M), match([B,C|R], RM).
mines(Count, Mines) :- same_length(Count, Mines), append([[o],Mines,[o]], M), match(M, Count).
sublist(L, M) :-
M = [_|_],
append(_, Suffix, L),
append(M, _, Suffix).
subseq([], []).
subseq([X|L], [X|M]) :- subseq(L, M).
subseq([_|L], M) :- subseq(L, M).
disjoint([], [], []).
module Main where
import Control.Monad
import Data.List
import Data.Maybe
modifyAt :: Int -> (a -> a) -> [a] -> [a]
modifyAt pos f l = ll ++ case lr of
[] -> []
x:xs -> f x:xs
import Data.List
data Prop
= Const Bool
| Var Char
| Not Prop
| And Prop Prop
| Or Prop Prop
deriving (Show)