Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
I can do it better... solution in Clear category for YAML. More Types by DEADPOOL
# Taken from mission YAML. Simple Dict
def yaml(a):
q=0
s=''
z={}
for i in a:
if i != '\n' and i != "\\":
s+=i
if i == '\n':
if s != '':
if s[s.index(':')+2] == '"' or s[s.index(':')+2] == "''":
s = s[0:s.index(':')+2]+s[s.index(':')+3:s.rfind('"')]
try:
z[s[:s.index(':')]]=int(s[s.index(':')+2:])
except ValueError:
if s[s.index(':')+2:].lower() == 'true':
z[s[:s.index(':')]]=True
if s[s.index(':')+2:].lower() == 'false':
z[s[:s.index(':')]]=False
else:
z[s[:s.index(':')]]=s[s.index(':')+2:]
s=''
try:
if s[s.index(':')+2] == '"' or s[s.index(':')+2] == "''":
s = s[0:s.index(':')+2]+s[s.index(':')+3:s.rfind('"')]
q = 1
except IndexError:
pass
try:
z[s[:s.index(':')]]=int(s[s.index(':')+2:])
except ValueError:
if s[s.index(':')+2:].lower() == 'true':
z[s[:s.index(':')]]=True
if s[s.index(':')+2:].lower() == 'false':
z[s[:s.index(':')]]=False
elif s[s.index(':')+2:].lower() == '' or (s[s.index(':')+2:].lower() == 'null' and q != 1):
z[s[:s.index(':')]]=None
else:
z[s[:s.index(':')]]=s[s.index(':')+2:]
return z
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
# return None
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 17, 2021
Comments: