Last active
September 30, 2019 11:41
-
-
Save MishraKhushbu/b0e014f8a722cfc520186b9953891d5c to your computer and use it in GitHub Desktop.
google python class day1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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/ | |
# Basic list exercises | |
# Fill in the code for the functions below. main() is already set up | |
# to call the functions with a few different inputs, | |
# printing 'OK' when each function is correct. | |
# The starter code for each function includes a 'return' | |
# which is just a placeholder for your code. | |
# It's ok if you do not complete all the functions, and there | |
# are some additional functions to try in list2.py. | |
# A. match_ends | |
# Given a list of strings, return the count of the number of | |
# strings where the string length is 2 or more and the first | |
# and last chars of the string are the same. | |
# Note: python does not have a ++ operator, but += works. | |
def match_ends(words): #(match_ends(['aba', 'xyz', 'aa', 'x', 'bbb']), 3) | |
m = 0 | |
for i in range (len(words)): | |
if(len(words[i]) >= 2): | |
p = len(words[i]) | |
if (words[i][0] == words[i][p-1]): | |
m = m+1 | |
print m | |
return | |
# B. front_x | |
# Given a list of strings, return a list with the strings | |
# in sorted order, except group all the strings that begin with 'x' first. | |
# e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields | |
# ['xanadu', 'xyz', 'aardvark', 'apple', 'mix'] | |
# Hint: this can be done by making 2 lists and sorting each of them | |
# before combining them. | |
def front_x(words): | |
a = [] | |
b = [] | |
k = 0 | |
for i in range (len(words)): | |
if words[i].startswith('x'): | |
a.append(words[i]) | |
else: | |
b.append(words[i]) | |
p = sorted(a) | |
q = sorted(b) | |
x = p+q | |
print x | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment