<commit_before>#!/usr/bin/python -tt
# Copyright 2010 Google Inc.
# Licensed under the Apache License, Version 2.0
# http://www.apache.org/licenses/LICENSE-2.0

# Google's Python Class
# http://code.google.com/edu/languages/google-python-class/

# Additional basic list exercises

# D. Given a list of numbers, return a list where
# all adjacent == elements have been reduced to a single element,
# so [1, 2, 2, 3] returns [1, 2, 3]. You may create a new list or
# modify the passed in list.
def remove_adjacent(nums):
  sololist = []
  for i in range(len(nums)-1):
    if nums[i] != nums[i+1]:
      sololist.append(nums[i])
  if sololist[-1:] != nums[-1:]:
    sololist.append(nums[-1])
  return sololist

  

# E. Given two lists sorted in increasing order, create and return a merged
# list of all the elements in sorted order. You may modify the passed in lists.
# Ideally, the solution should work in "linear" time, making a single
# pass of both lists.
#(linear_merge(['aa', 'xx', 'zz'], ['bb', 'cc']),
       #['aa', 'bb', 'cc', 'xx', 'zz'])
def linear_merge(list1, list2):
  merged = []
  # len1 = len(list1)
  # len2 = len(list2)
  while len(list1) > 0 and len(list2) > 0:
    if(list1[0] < list2[0]):
      merged.append(list1.pop(0))
    else:
      merged.append(list2.pop(0))
  if len(list1) > 0:
    merged.extend(list1)
  else:
    merged.extend(list2)
  return merged
  #return sorted(list1 + list2) the easy way, but slower

# Note: the solution above is kind of cute, but unforunately list.pop(0)
# is not constant time with the standard python list implementation, so
# the above is not strictly linear time.
# An alternate approach uses pop(-1) to remove the endmost elements
# from each list, building a solution list which is backwards.
# Then use reversed() to put the result back in the correct order. That
# solution works in linear time, but is more ugly.


# Simple provided test() function used in main() to print
# what each function returns vs. what it's supposed to return.
def test(got, expected):
  if got == expected:
    prefix = ' OK '
  else:
    prefix = '  X '
  print ('%s got: %s expected: %s' % (prefix, repr(got), repr(expected)))

<commit_msg>Rewrote remove_adjacent to not be terrible, removed unneeded code from merge<commit_after>#!/usr/bin/python -tt
# Copyright 2010 Google Inc.
# Licensed under the Apache License, Version 2.0
# http://www.apache.org/licenses/LICENSE-2.0

# Google's Python Class
# http://code.google.com/edu/languages/google-python-class/

# Additional basic list exercises

# D. Given a list of numbers, return a list where
# all adjacent == elements have been reduced to a single element,
# so [1, 2, 2, 3] returns [1, 2, 3]. You may create a new list or
# modify the passed in list.
def remove_adjacent(nums):
  sololist = []
  for num in nums:
    if len(sololist) == 0 or num != sololist[-1]:
      sololist.append(num)
  return sololist

  

# E. Given two lists sorted in increasing order, create and return a merged
# list of all the elements in sorted order. You may modify the passed in lists.
# Ideally, the solution should work in "linear" time, making a single
# pass of both lists.
#(linear_merge(['aa', 'xx', 'zz'], ['bb', 'cc']),
       #['aa', 'bb', 'cc', 'xx', 'zz'])
def linear_merge(list1, list2):
  merged = []
  # len1 = len(list1)
  # len2 = len(list2)
  while len(list1) and len(list2):
    if(list1[0] < list2[0]):
      merged.append(list1.pop(0))
    else:
      merged.append(list2.pop(0))
  merged.extend(list1)
  merged.extend(list2)
  return merged
  #return sorted(list1 + list2) the easy way, but slower

# Note: the solution above is kind of cute, but unforunately list.pop(0)
# is not constant time with the standard python list implementation, so
# the above is not strictly linear time.
# An alternate approach uses pop(-1) to remove the endmost elements
# from each list, building a solution list which is backwards.
# Then use reversed() to put the result back in the correct order. That
# solution works in linear time, but is more ugly.


# Simple provided test() function used in main() to print
# what each function returns vs. what it's supposed to return.
def test(got, expected):
  if got == expected:
    prefix = ' OK '
  else:
    prefix = '  X '
  print ('%s got: %s expected: %s' % (prefix, repr(got), repr(expected)))


{'fraction_non_alphanumeric': 0.12211981566820276, 'fraction_numerical': 0.019585253456221197, 'mean_word_length': 3.6427807486631014, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6835772', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1375, 'n_words': 559}