Linguist 278: Programming for Linguists (Stanford Linguistics, Fall 2021)

Class 4: Review exercises

# Paste this in:
s = "Colorless green ideas"


# Measure the length of the str s:


# What will this code do?
list(s)



# Remove all the spaces from s:


# Paste this in:
tree = ['NP', ['D', ['the']], ['N', ['code']]]


# Index into tree to pull out the str 'the':


# Index into tree to pull out the list ['N', ['code']]:



# Index into tree to pull out the list ['code']:



# Index into tree to pull out the str 'code':



# Methods can be stacked if they have the right output values.
# For example, "ABC".lower().split("b") returns ["a", 'c"].
# Your task: in a single line, map "ABC" to "aDc":



# Write a function `rev` that reverses its input string
# using a `for`-loop.


# How many ways can you think of to implement a function that, when
# given a list `x`, returns `x` in sorted order, with all its
# duplicates removed?



# Write a function `counter` that, given a list, returns a dict
# mapping the unique elements of that list to the number of times
# they appear in the list. For example, given
#
# vals = ["a", "a", "b", "c"]
#
# The function should return {"a": 2, "b": 1, "c": 1}