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 weiching.ng.chia
# Taken from mission YAML. Simple Dict
def yaml(a):
# your code here
dict = {}
for line in a.splitlines():
x = line.split(':')
if len(x) == 2:
key = x[0].strip()
value = x[1].strip()
dict[key] = int(value) if value.isdigit() else value
return dict
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
dict = {}
for line in a.splitlines():
x = line.split(':')
if len(x) == 2:
key = x[0].strip()
value = x[1].strip().replace('\\', '')
if value.startswith('"'):
value = value[1:-1:]
elif value.isdigit():
value = int(value)
elif value.lower() == 'true':
value = True
elif value.lower() == 'false':
value = False
elif len(value) == 0 or value.lower() == 'null':
value = None
dict[key] = value
print(dict)
return dict
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!")
April 27, 2021