PKU_NLPDL_Assignment1 / task1 /submission.py
Antoine Li
Upload folder using huggingface_hub
0ee6a96 verified
Raw
History Blame Contribute Delete
629 Bytes
def flatten_list(nested_list):
flat_list = []
stack = [nested_list]
while stack:
current = stack.pop()
if isinstance(current, list):
stack.extend(reversed(current))
else:
flat_list.append(current)
return flat_list
def char_count(s: str):
if not s:
return {}
char_dict = {}
for char in s:
char_dict[char] = char_dict.get(char, 0) + 1
return char_dict
if __name__ == "__main__":
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(flatten_list(nested_list))
s = "hello world"
print(char_count(s))