Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for YAML. More Types by hilib
# Taken from mission YAML. Simple Dict
def yaml(a):
# your code here
b = a.split("\n")
d ={}
for c in b :
if c == "":
continue
else:
i= c.split(": ")
if i[1].isdigit():
i[1] =int(i[1])
d[i[0]] = i[1]
else:
d[i[0]] = i[1]
return d
if __name__ == '__main__':
print("Example:")
print(yaml("""name: Alex
age: 12"""))
# These "asserts" are used for self-checking and not for an auto-testing
assert yaml("""name: Alex
age: 12""") == {'age': 12, 'name': 'Alex'}
assert yaml("""name: Alex Fox
age: 12
class: 12b""") == {'age': 12,
'class': '12b',
'name': 'Alex Fox'}
print("Coding complete? Click 'Check' to earn cool rewards!")
def yaml(a):
# your code here
b = a.split("\n\n")
d ={}
for i in b :
if "\n" not in i :
f= i.split(":")
if f[1].strip().isdigit():
f[1] = int(f[1])
d[f[0]] = f[1]
else:
d[f[0]] = f[1].strip()
else:
c = sorted(i.split("\n"))
for j in c :
if j =="":
continue
else:
e= j.split(":")
if e[1].strip().isdigit():
e[1] = int(e[1])
d[e[0]] = e[1]
else:
if e[1].strip() =="false":
e[1] = False
d[e[0]] =e[1]
elif e[1].strip() == "true":
e[1] = True
d[e[0]] = e[1]
elif '"' in e[1]:
d[e[0]] = eval(e[1])
elif e[1].strip() == "null" or e[1].strip() =="" :
e[1] = None
d[e[0]] =e[1]
else:
d[e[0]] = e[1].strip()
return d
if __name__ == '__main__':
print("Example:")
print(yaml('name: Alex\nage: 12'))
# These "asserts" are used for self-checking and not for an auto-testing
assert yaml('name: Alex\nage: 12') == {'age': 12, 'name': 'Alex'}
assert yaml('name: Alex Fox\n'
'age: 12\n'
'\n'
'class: 12b') == {'age': 12,
'class': '12b',
'name': 'Alex Fox'}
assert yaml('name: "Alex Fox"\n'
'age: 12\n'
'\n'
'class: 12b') == {'age': 12,
'class': '12b',
'name': 'Alex Fox'}
assert yaml('name: "Alex \\"Fox\\""\n'
'age: 12\n'
'\n'
'class: 12b') == {'age': 12,
'class': '12b',
'name': 'Alex "Fox"'}
assert yaml('name: "Bob Dylan"\n'
'children: 6\n'
'alive: false') == {'alive': False,
'children': 6,
'name': 'Bob Dylan'}
assert yaml('name: "Bob Dylan"\n'
'children: 6\n'
'coding:') == {'children': 6,
'coding': None,
'name': 'Bob Dylan'}
assert yaml('name: "Bob Dylan"\n'
'children: 6\n'
'coding: null') == {'children': 6,
'coding': None,
'name': 'Bob Dylan'}
assert yaml('name: "Bob Dylan"\n'
'children: 6\n'
'coding: "null" ') == {'children': 6,
'coding': 'null',
'name': 'Bob Dylan'}
print("Coding complete? Click 'Check' to earn cool rewards!")
June 12, 2020