Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Google's basic python class basic exercises #339

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions _sources/index_en.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Contents:
quiz/Quiz13_en.rst
quiz/QuizExtras_en.rst
quiz/QuizExtras2_en.rst
quiz/BasicQuiz_en.rst
challenges/Reto01_en.rst
challenges/Reto02.rst
challenges/Reto03_en.rst
Expand Down
1 change: 1 addition & 0 deletions _sources/index_es.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Contenidos:
quiz/Quiz13.rst
quiz/QuizExtras.rst
quiz/QuizExtras2.rst
quiz/BasicQuiz.rst
challenges/Reto01.rst
challenges/Reto02.rst
challenges/Reto03.rst
Expand Down
180 changes: 180 additions & 0 deletions _sources/quiz/BasicQuiz.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
==================
Ejercicios básicos
==================

+ Ejercicios básicos de la clase de Python de Google

.. |br| raw:: html

<br />


.. tabbed:: basicExercises

.. tab:: Ejercicio 1

.. activecode:: basic_q1es
:nocodelens:

Dada una lista de cadenas, devuelve una lista con las cadenas en orden ordenado, excepto agrupa todas las cadenas que comienzan con 'x' primero. Por ejemplo: ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] produce ['xanadu', 'xyz', 'aardvark', 'apple', 'mix'] |br|

~~~~
def front_x(words):

====

from unittest.gui import TestCaseGui

class myTests(TestCaseGui):
def testOne(self):
self.assertEqual(front_x(['bbb', 'ccc', 'axx', 'xzz', 'xaa']), ['xaa', 'xzz', 'axx', 'bbb', 'ccc'], "Esperado: ['xaa', 'xzz', 'axx', 'bbb', 'ccc']")
self.assertEqual(front_x(['ccc', 'bbb', 'aaa', 'xcc', 'xaa']), ['xaa', 'xcc', 'aaa', 'bbb', 'ccc'], "Esperado: ['xaa', 'xcc', 'aaa', 'bbb', 'ccc']")
self.assertEqual(front_x(['mix', 'xyz', 'apple', 'xanadu', 'aardvark']), ['xanadu', 'xyz', 'aardvark', 'apple', 'mix'], "Esperado: ['xanadu', 'xyz', 'aardvark', 'apple', 'mix']")

myTests().main()



.. tab:: Ejercicio 2

.. activecode:: basic_q2es
:nocodelens:

Dada una lista de cadenas, devuelve el recuento del número de cadenas donde la longitud de la cadena es 2 o más y el primer y último caracteres de la cadena son iguales. |br|

~~~~
def match_ends(words):


====
from unittest.gui import TestCaseGui

class MyTests(TestCaseGui):
def testOne(self):
self.assertEqual(match_ends(['aba', 'xyz', 'aa', 'x', 'bbb']), 3, "Esperado: 3")
self.assertEqual(match_ends(['', 'x', 'xy', 'xyx', 'xx']), 2, "Esperado: 2")
self.assertEqual(match_ends(['aaa', 'be', 'abc', 'hello']), 1, "Esperado: 1")

MyTests().main()


.. tab:: Ejercicio 3

.. activecode:: basic_q3es
:nocodelens:

Dada una lista de tuplas no vacías, devuelve una lista ordenada en orden creciente por el último elemento en cada tupla. |br|

~~~~
# Extraer el último elemento de una tupla, utilizado para ordenación personalizada a continuación
def last(a):

def sort_last(tuples):

====
from unittest.gui import TestCaseGui

class myTests(TestCaseGui):
def testOne(self):
self.assertEqual(sort_last([(1, 3), (3, 2), (2, 1)]), [(2, 1), (3, 2), (1, 3)], "Esperado: [(2, 1), (3, 2), (1, 3)]")
self.assertEqual(sort_last([(2, 3), (1, 2), (3, 1)]), [(3, 1), (1, 2), (2, 3)], "Esperado: [(3, 1), (1, 2), (2, 3)]")
self.assertEqual(sort_last([(1, 7), (1, 3), (3, 4, 5), (2, 2)]), [(2, 2), (1, 3), (3, 4, 5), (1, 7)], "Esperado: [(2, 2), (1, 3), (3, 4, 5), (1, 7)]")

myTests().main()



.. tab:: Ejercicio 4

.. activecode:: basic_q4es
:nocodelens:

Dado un entero que representa el número de donuts, devuelve una cadena de la forma 'Número de donuts: <count>', donde <count> es el número pasado. Sin embargo, si el recuento es 10 o más, use la palabra 'muchos' en lugar del recuento real. |br|

~~~~
def donuts(count):

====
from unittest.gui import TestCaseGui

class myTests(TestCaseGui):
def testOne(self):
self.assertEqual(donuts(4), 'Número de donuts: 4', "Esperado: 'Número de donuts: 4'")
self.assertEqual(donuts(9), 'Número de donuts: 9', "Esperado: 'Número de donuts: 9'")
self.assertEqual(donuts(10), 'Número de donuts: muchos', "Esperado: 'Número de donuts: muchos'")
self.assertEqual(donuts(99), 'Número de donuts: muchos', "Esperado: 'Número de donuts: muchos'")

myTests().main()



.. tab:: Ejercicio 5

.. activecode:: basic_q5es
:nocodelens:

Dada una cadena s, devuelve una cadena hecha de los primeros 2 y los últimos 2 caracteres de la cadena original, por lo que 'spring' produce 'spng'. Sin embargo, si la longitud de la cadena es menor que 2, devuelva en su lugar la cadena vacía. |br|

~~~~
def both_ends(s):

====
from unittest.gui import TestCaseGui

class myTests(TestCaseGui):
def testOne(self):
self.assertEqual(both_ends('spring'), 'spng', "Esperado: 'spng'")
self.assertEqual(both_ends('Hello'), 'Helo', "Esperado: 'Helo'")
self.assertEqual(both_ends('a'), '', "Esperado: ''")
self.assertEqual(both_ends('xyz'), 'xyyz', "Esperado: 'xyyz'")

myTests().main()




.. tab:: Ejercicio 6

.. activecode:: basic_q6es
:nocodelens:

Dada una cadenas, devuelve una cadena donde todas las ocurrencias de su primer carácter se han cambiado a '*', excepto no cambie el primer carácter en sí. Por ejemplo, 'babble' produce 'ba**le'. Suponga que la cadena tiene una longitud de 1 o más. |br|

~~~~
def fix_start(s):

====
from unittest.gui import TestCaseGui

class myTests(TestCaseGui):
def testOne(self):
self.assertEqual(fix_start('babble'), 'ba**le', "Esperado: 'ba**le'")
self.assertEqual(fix_start('aardvark'), 'a*rdv*rk', "Esperado: 'a*rdv*rk'")
self.assertEqual(fix_start('google'), 'goo*le', "Esperado: 'goo*le'")
self.assertEqual(fix_start('donut'), 'donut', "Esperado: 'donut'")

myTests().main()



.. tab:: Ejercicio 7

.. activecode:: basic_q7es
:nocodelens:

Dadas las cadenas a y b, devuelve una sola cadena con a y b separadas por un espacio '<a> <b>', excepto que intercambie los dos primeros caracteres de cada cadena. |br|

~~~~
def mix_up(a, b):

====
from unittest.gui import TestCaseGui

class myTests(TestCaseGui):
def testOne(self):
self.assertEqual(mix_up('mix', 'pod'), 'pox mid', "Esperado: 'pox mid'")
self.assertEqual(mix_up('dog', 'dinner'), 'dig donner', "Esperado: 'dig donner'")
self.assertEqual(mix_up('gnash', 'sport'), 'spash gnort', "Esperado: 'spash gnort'")
self.assertEqual(mix_up('pezzy', 'firm'), 'fizzy perm', "Esperado: 'fizzy perm'")

myTests().main()
180 changes: 180 additions & 0 deletions _sources/quiz/BasicQuiz_en.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
===============
Basic Exercises
===============

+ Basic Exercises from Google's Python Class

.. |br| raw:: html

<br />


.. tabbed:: basicExercises

.. tab:: Exercise 1

.. activecode:: basic_q1
:nocodelens:

Given a list of strings, return a list with the strings in sorted order, except group all the strings that begin with 'x' first. For example: ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields ['xanadu', 'xyz', 'aardvark', 'apple', 'mix'] |br|

~~~~
def front_x(words):

====

from unittest.gui import TestCaseGui

class myTests(TestCaseGui):
def testOne(self):
self.assertEqual(front_x(['bbb', 'ccc', 'axx', 'xzz', 'xaa']), ['xaa', 'xzz', 'axx', 'bbb', 'ccc'], "Expected: ['xaa', 'xzz', 'axx', 'bbb', 'ccc']")
self.assertEqual(front_x(['ccc', 'bbb', 'aaa', 'xcc', 'xaa']), ['xaa', 'xcc', 'aaa', 'bbb', 'ccc'], "Expected: ['xaa', 'xcc', 'aaa', 'bbb', 'ccc']")
self.assertEqual(front_x(['mix', 'xyz', 'apple', 'xanadu', 'aardvark']), ['xanadu', 'xyz', 'aardvark', 'apple', 'mix'], "Expected: ['xanadu', 'xyz', 'aardvark', 'apple', 'mix']")

myTests().main()



.. tab:: Exercise 2

.. activecode:: basic_q2
:nocodelens:

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. |br|

~~~~
def match_ends(words):


====
from unittest.gui import TestCaseGui

class MyTests(TestCaseGui):
def testOne(self):
self.assertEqual(match_ends(['aba', 'xyz', 'aa', 'x', 'bbb']), 3, "Expected: 3")
self.assertEqual(match_ends(['', 'x', 'xy', 'xyx', 'xx']), 2, "Expected: 2")
self.assertEqual(match_ends(['aaa', 'be', 'abc', 'hello']), 1, "Expected: 1")

MyTests().main()


.. tab:: Exercise 3

.. activecode:: basic_q3
:nocodelens:

Given a list of non-empty tuples, return a list sorted in increasing order by the last element in each tuple. |br|

~~~~
# Extract the last element from a tuple -- used for custom sorting below.
def last(a):

def sort_last(tuples):

====
from unittest.gui import TestCaseGui

class myTests(TestCaseGui):
def testOne(self):
self.assertEqual(sort_last([(1, 3), (3, 2), (2, 1)]), [(2, 1), (3, 2), (1, 3)], "Expected: [(2, 1), (3, 2), (1, 3)]")
self.assertEqual(sort_last([(2, 3), (1, 2), (3, 1)]), [(3, 1), (1, 2), (2, 3)], "Expected: [(3, 1), (1, 2), (2, 3)]")
self.assertEqual(sort_last([(1, 7), (1, 3), (3, 4, 5), (2, 2)]), [(2, 2), (1, 3), (3, 4, 5), (1, 7)], "Expected: [(2, 2), (1, 3), (3, 4, 5), (1, 7)]")

myTests().main()



.. tab:: Exercise 4

.. activecode:: basic_q4
:nocodelens:

Given an int count of a number of donuts, return a string of the form 'Number of donuts: <count>', where <count> is the number passed in. However, if the count is 10 or more, then use the word 'many' instead of the actual count. |br|

~~~~
def donuts(count):

====
from unittest.gui import TestCaseGui

class myTests(TestCaseGui):
def testOne(self):
self.assertEqual(donuts(4), 'Number of donuts: 4', "Expected: 'Number of donuts: 4'")
self.assertEqual(donuts(9), 'Number of donuts: 9', "Expected: 'Number of donuts: 9'")
self.assertEqual(donuts(10), 'Number of donuts: many', "Expected: 'Number of donuts: many'")
self.assertEqual(donuts(99), 'Number of donuts: many', "Expected: 'Number of donuts: many'")

myTests().main()



.. tab:: Exercise 5

.. activecode:: basic_q5
:nocodelens:

Given a string s, return a string made of the first 2 and the last 2 chars of the original string, so 'spring' yields 'spng'. However, if the string length is less than 2, return instead the empty string. |br|

~~~~
def both_ends(s):

====
from unittest.gui import TestCaseGui

class myTests(TestCaseGui):
def testOne(self):
self.assertEqual(both_ends('spring'), 'spng', "Expected: 'spng'")
self.assertEqual(both_ends('Hello'), 'Helo', "Expected: 'Helo'")
self.assertEqual(both_ends('a'), '', "Expected: ''")
self.assertEqual(both_ends('xyz'), 'xyyz', "Expected: 'xyyz'")

myTests().main()




.. tab:: Exercise 6

.. activecode:: basic_q6
:nocodelens:

Given a string s, return a string where all occurrences of its first char have been changed to '*', except do not change the first char itself. For example, 'babble' yields 'ba**le'. Assume that the string is length 1 or more. |br|

~~~~
def fix_start(s):

====
from unittest.gui import TestCaseGui

class myTests(TestCaseGui):
def testOne(self):
self.assertEqual(fix_start('babble'), 'ba**le', "Expected: 'ba**le'")
self.assertEqual(fix_start('aardvark'), 'a*rdv*rk', "Expected: 'a*rdv*rk'")
self.assertEqual(fix_start('google'), 'goo*le', "Expected: 'goo*le'")
self.assertEqual(fix_start('donut'), 'donut', "Expected: 'donut'")

myTests().main()



.. tab:: Exercise 7

.. activecode:: basic_q7
:nocodelens:

Given strings a and b, return a single string with a and b separated by a space '<a> <b>', except swap the first 2 chars of each string. |br|

~~~~
def mix_up(a, b):

====
from unittest.gui import TestCaseGui

class myTests(TestCaseGui):
def testOne(self):
self.assertEqual(mix_up('mix', 'pod'), 'pox mid', "Expected: 'pox mid'")
self.assertEqual(mix_up('dog', 'dinner'), 'dig donner', "Expected: 'dig donner'")
self.assertEqual(mix_up('gnash', 'sport'), 'spash gnort', "Expected: 'spash gnort'")
self.assertEqual(mix_up('pezzy', 'firm'), 'fizzy perm', "Expected: 'fizzy perm'")

myTests().main()
Loading