_id
stringlengths 2
5
| title
stringlengths 2
76
| partition
stringclasses 3
values | text
stringlengths 8
6.97k
| language
stringclasses 1
value | meta_information
dict |
---|---|---|---|---|---|
q1330 | Increment a numerical string | valid | next = str(int('123') + 1)
| Python | {
"resource": ""
} |
q1331 | Strip a set of characters from a string | valid | >>> def stripchars(s, chars):
... return s.translate(None, chars)
...
>>> stripchars("She was a soul stripper. She took my heart!", "aei")
'Sh ws soul strppr. Sh took my hrt!'
| Python | {
"resource": ""
} |
q1332 | Sorting algorithms_Permutation sort | valid | from itertools import permutations
in_order = lambda s: all(x <= s[i+1] for i,x in enumerate(s[:-1]))
perm_sort = lambda s: (p for p in permutations(s) if in_order(p)).next()
| Python | {
"resource": ""
} |
q1333 | Averages_Arithmetic mean | valid | from math import fsum
def average(x):
return fsum(x)/float(len(x)) if x else 0
print (average([0,0,3,1,4,1,5,9,0,0]))
print (average([1e20,-1e-20,3,1,4,1,5,9,-1e20,1e-20]))
| Python | {
"resource": ""
} |
q1334 | Abbreviations, simple | valid | command_table_text =
user_words = "riG rePEAT copies put mo rest types fup. 6 poweRin"
def find_abbreviations_length(command_table_text):
command_table = dict()
input_iter = iter(command_table_text.split())
word = None
try:
while True:
if word is None:
word = next(input_iter)
abbr_len = next(input_iter, len(word))
try:
command_table[word] = int(abbr_len)
word = None
except ValueError:
command_table[word] = len(word)
word = abbr_len
except StopIteration:
pass
return command_table
def find_abbreviations(command_table):
abbreviations = dict()
for command, min_abbr_len in command_table.items():
for l in range(min_abbr_len, len(command)+1):
abbr = command[:l].lower()
abbreviations[abbr] = command.upper()
return abbreviations
def parse_user_string(user_string, abbreviations):
user_words = [word.lower() for word in user_string.split()]
commands = [abbreviations.get(user_word, "*error*") for user_word in user_words]
return " ".join(commands)
command_table = find_abbreviations_length(command_table_text)
abbreviations_table = find_abbreviations(command_table)
full_words = parse_user_string(user_words, abbreviations_table)
print("user words:", user_words)
print("full words:", full_words)
| Python | {
"resource": ""
} |
q1335 | Entropy | valid | from __future__ import division
import math
def hist(source):
hist = {}; l = 0;
for e in source:
l += 1
if e not in hist:
hist[e] = 0
hist[e] += 1
return (l,hist)
def entropy(hist,l):
elist = []
for v in hist.values():
c = v / l
elist.append(-c * math.log(c ,2))
return sum(elist)
def printHist(h):
flip = lambda (k,v) : (v,k)
h = sorted(h.iteritems(), key = flip)
print 'Sym\thi\tfi\tInf'
for (k,v) in h:
print '%s\t%f\t%f\t%f'%(k,v,v/l,-math.log(v/l, 2))
source = "1223334444"
(l,h) = hist(source);
print '.[Results].'
print 'Length',l
print 'Entropy:', entropy(h, l)
printHist(h)
| Python | {
"resource": ""
} |
q1336 | Tokenize a string with escaping | valid | def token_with_escape(a, escape = '^', separator = '|'):
result = []
token = ''
state = 0
for c in a:
if state == 0:
if c == escape:
state = 1
elif c == separator:
result.append(token)
token = ''
else:
token += c
elif state == 1:
token += c
state = 0
result.append(token)
return result
| Python | {
"resource": ""
} |
q1337 | Hello world_Text | valid | print "Hello world!"
| Python | {
"resource": ""
} |