0% found this document useful (0 votes)
42 views

Taller 1

Uploaded by

mariafernandahh2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Taller 1

Uploaded by

mariafernandahh2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Universidad Industrial de Santander E3T Programación de Computadores II

Workshop - Parcial 1
(Update March 9, 2024)

Instructions:
To solve these exercises, limit your references to the following two pages:
• https://www.w3schools.com/python/
• https://docs.python.org/3/tutorial/
You also can use Pythontutor to debug your code:
• https://pythontutor.com/python-compiler.html

Part 1: Beginner Level

1
Create a program that receives two numbers and then indicates which number is larger or if both
are equal.

Test:
Input : a = 5 , b = 10
Output : Second number i s l a r g e r

Input : a = 1 0 , b = 4
Output : F i r s t number i s l a r g e r

Input : a = 1 , b = 1
Output : Both numbers are equal

2
Create a program that checks if a word is a palindrome or not. In other words, it should be the
same word when read from front to back and back to front. You should present two solutions: a
loop approach version and a method approach version.
Test:
a = " arenera "
return True
a = " Hola "
return False

3
Create a program that receives an integer and prints whether it is a prime number or not.

Test:
a=37
return 37 i s a prime number
a=10
return 10 i s not a prime number

4
Create a program that receives an integer n and prints a right triangle with a base length equal to
n.

Test:
a = 5
return :

2024
Universidad Industrial de Santander E3T Programación de Computadores II

*
**
***
****
*****

5
Given two lists nums1 and nums2, return a list of their intersection. Each element in the result must
be unique and you may return the result in any order.

Test:
Input : num1 = [ 1 , 2 , 2 , 1 ] , num2 = [ 2 , 2 ]
Output : [ 2 ]

Input : num1 = [ 4 , 9 , 5 ] , num2 = [ 9 , 4 , 9 , 8 , 4 ]


Output : [ 9 , 4 ]
Output : [ 4 , 9 ] i s a l s o accepted .

Part 2: Intermediate Level

6
Create a program that can check a password, where the conditions are:
• Must contain at least one capital letter.

• Must contain at least one lowercase letter.


• Must contain at least one of the special characters: *, -, &, %, @
• Must contain at least 1 number.
• Must not contain any spaces

Test:
Input : a = " hola "
Output : Please , write another password

Input : a = " Progr@macion&2"


Output : The password i s w e l l done

7
Create a program that receives a positive integer and returns its factorial.

Test:
a = 6
return 720

8
Create a program that receives a list of numbers and returns their average

Test:
a = [10 ,2 ,3]
return 5 . 0

2024
Universidad Industrial de Santander E3T Programación de Computadores II

9
Create a program that receives the value of x and the number of terms, and returns the same
number of terms in the Taylor series for the exponential function (Maclaurin Serie). Note: create a
factorial function (exercise 7) and use it for the solution.

xn x2 x3 x4
ex = ∑ n!
= 1+x+
2!
+
3!
+
4!
+...
n =0

Test:
a = 5 , b = 10
return [ 1 . 0 , 5 . 0 , 1 2 . 5 , 2 0 . 8 3 , 26.04 , 26.04 , 2 1 . 7 , 1 5 . 5 , 9 . 6 9 , 5 . 3 8 ]

10
Write a Python function that takes a list of integers as input and returns the list sorted in ascending
order. In this exercise, you are not allowed to use the sort() method.
def s o r t _ l i s t ( l i s t _ 1 ) :
#Code h e r e
return l i s t _ 1 # S o r t e d .
Test:
Input : s o r t _ l i s t ( [ 1 , 1 0 , 6 , 8 , 7 , 5 ] )
Output : [ 1 , 5 , 6 , 7 , 8 , 1 0 ]

11
Modify the previous exercise, by including a boolean parameter called "reverse" that indicates
whether to sort the list in ascending (True) or descending (False) order.
def s o r t _ l i s t ( l i s t _ 1 , reverse ) :
#Code h e r e
return l i s t _ 1 # S o r t e d d e p e n d i n g on t h e r e v e r s e p a r a m e t e r
Test:
Input : s o r t _ l i s t ( [ 1 , 1 0 , 6 , 8 , 7 , 5 ] , reverse=True )
Output : [ 1 , 5 , 6 , 7 , 8 , 1 0 ]

Input : s o r t _ l i s t ( [ 1 , 1 0 , 6 , 8 , 7 , 5 ] , reverse=False )
Output : [ 1 0 , 8 , 7 , 6 , 5 , 1 ]

12
Create a program that receives a list nums, for each num in nums find out how many numbers in the
list are smaller than it. Return the answer in a list.
Example 1 :
Input : nums = [ 8 , 1 , 2 , 2 , 3 ]
Output : [ 4 , 0 , 1 , 1 , 3 ]
Explanation:
• For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3).

• For nums[1]=1 does not exist any smaller number than it.
• For nums[2]=2 there exist one smaller number than it (1).
• For nums[3]=2 there exist one smaller number than it (1).
• For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).

2024
Universidad Industrial de Santander E3T Programación de Computadores II

13
The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding
ones, starting with 0 and 1. For example, the first few numbers in the Fibonacci series are 0, 1, 1,
2, 3, 5, 8, 13, 21, and so on. Define a function named generate_fibonacci_series that takes a single
parameter, limit, representing the upper limit up to which the Fibonacci series should be generated
def g e n e r a t e _ f i b o n a c c i _ s e r i e s ( l i m i t ) :
# code h e r e
return f i b o n a c c i _ s e r i e s
Test:
Input : g e n e r a t e _ f i b o n a c c i _ s e r i e s ( 1 0 )
Output : [ 0 , 1 , 1 , 2 , 3 , 5 , 8 , 1 3 , 2 1 , 3 4 ]

Input : g e n e r a t e _ f i b o n a c c i _ s e r i e s ( 7 )
Output : [ 0 , 1 , 1 , 2 , 3 , 5 , 8 ]

14
Write a Python function called sort_names_scores that sorts two given lists, one containing names
and the other containing corresponding scores, in ascending order based on the scores.
def sort_names_scores ( names , scores ) :
# code h e r e
return l i s t ( sorted_names ) , l i s t ( sorted_scores )
Test:
Input :
a = [ Juan , Pablo , Carolina ]
b = [100 ,300 ,50]
Output :
[ Pablo , Juan , Carolina ]
[300 ,100 ,50]

Input :
a = [ Carla , Brayan , Zhara ]
b = [200 ,30 ,150]
Output :
[ Carla , Zhara , Brayan ]
[200 ,150 ,30]

15
Create a function called sum_2d that takes a two-dimensional list as input and returns two lists: one
with the sum of each row (list_rows) and another with the sum of each column (list_column).
def sum_2d ( l i s t 2 d ) :
# code h e r e
return l i s t _ r o w (sum of each row ) , l i s t _ c o l u m n ( sum of each column )
Test:
Input :
list2d = [[2 ,3 , −1] ,
[5 ,4 ,6]]
Output :
list_row = [ 4 , 1 5 ]
list_column = [ 7 , 7 , 5 ]

Part 3: Challenge Level

2024
Universidad Industrial de Santander E3T Programación de Computadores II

16
Write a program that receives a sorted list, and removes some duplicates such that each unique
element appears at most twice. The relative order of the elements should be kept the same.
Test:
Input : [ 1 , 1 , 1 , 2 , 2 , 3 ]
Output : [ 1 , 1 , 2 , 2 , 3 ]

Input : [ 0 , 0 , 1 , 1 , 1 , 1 , 2 , 3 , 3 ]
Output : [ 0 , 0 , 1 , 1 , 2 , 3 , 3 ]

17
Create a program that performs matrix multiplication. The program receives two lists. The pro-
gram must check if the matrices are valid for the process, if so, it must return the multiplication,
otherwise, it must display the message: "It is not possible to operate".

Test:
Input :
a = [[2 ,1 ,3] ,[5 ,6 ,8]]
b = [[1 ,0] ,[1 ,2] ,[1 ,1]]
Output :
[[6 , 5] , [19 , 20]]

Input :
a = [[2 ,0 ,1] ,[5 ,1 ,1]]
b = [[1 ,1 ,1]]
Output : Error the dimensions do not match

18
Given a list of integers nums and an integer called target, return indices of the two numbers such
that they add up to target.

Test:
Input : nums = [ 2 , 7 , 1 1 , 1 5 ] , t a r g e t = 9
Output : [ 0 , 1 ]
Explanation : Because nums [ 0 ] + nums [ 1 ] == 9 , we return [ 0 , 1 ] .

Input : nums = [ 3 , 2 , 4 , 2 ] , t a r g e t = 6
Output : [ 1 , 2 ]

Input : nums = [ 3 , 3 ] , t a r g e t = 6
Output : [ 0 , 1 ]

19
Create a function program that gives the possible numerical positions where the knight can be
placed when adding a given position of a chessboard (as a number as (4,4) represented in Fig. 1).
(As represented in Fig. 1 the result will be: (2,3), (2,5), (3,2), (3,6), (5,2), (5,6), (6,3), (6,5)). Test:
Input :
a = 4
b = 4
Output : [ [ 2 , 3 ] , [ 2 , 5 ] , [ 3 , 2 ] , [ 3 , 6 ] , [ 5 , 2 ] , [ 5 , 6 ] , [ 6 , 3 ] , [ 6 , 5 ] ]

2024
Universidad Industrial de Santander E3T Programación de Computadores II

Figure 1: Chessboard position

20
Determine if a complete 9 x 9 Sudoku board is valid. To be valid, it must adhere to the following
rules:

• Each row must contain the digits 1-9 without repetition.


• Each column must contain the digits 1-9 without repetition.
• Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.
Note: A Sudoku board is considered valid only if it satisfies the aforementioned rules.
Test :
Input : Check Figure 2
board = [
[ " 5 " , "3" , "4" , "6" , "7" , "8" , "9" ,"1" , "2" ] ,
[ "6" , "7" , "2" , " 1 " , "9" , "5" , "3" , "4" , "8" ] ,
[ " 1 " , "9" , "8" , "3" , "4" , "2" , "5" , "6" , "7" ] ,
[ "8" , " 5 " , "9" , "7" , "6" ,"1" , "4" , "2" , "3" ] ,
[ "4" , "2" , "6" , "8" , " 5 " , "3" , "7" , "9" ,"1"] ,
[ "7" , " 1 " , "3" , "9" , "2" , "4" , "8" , "5" , "6" ] ,
[ "9" , "6" , " 1 " , " 5 " , "3" , "7" , "2" , "8" , "4" ] ,
[ "2" , "8" , "7" , "4" , " 1 " , "9" , "6" , "3" , "5" ] ,
[ "3" , "4" , "5" , "2" , "8" , "6" ,"1" , "7" , "9" ]
]

Output : The sudoku i s v a l i d


Input : Check Figure 3
board = [
[ " 5 " , "3" , "4" , "6" , "7" , "8" , "9" ,"1" , "2" ] ,
[ "6" , "7" , "2" , " 1 " , "9" , " 5 " , "3" , "4" , "8" ] ,
[ " 1 " , "9" , "8" , "3" , "4" , "2" , "5" , "6" , "7" ] ,
[ "8" , " 5 " , "9" , "7" , "6" , " 1 " , "4" , "2" , "3" ] ,
[ "4" , "2" , "6" , "8" , " 5 " , "3" , "7" , "9" ,"1"] ,
[ "7" , " 1 " , "3" , "9" , "2" , "4" , "8" , "5" , "6" ] ,
[ "9" , "6" , " 1 " , " 5 " , "3" , "7" , "2" , "8" , "4" ] ,
[ "2" , "8" , "7" , "4" , " 1 " , "9" , "6" , "3" , "5" ] ,
[ "3" , "4" , " 5 " , "2" , "8" , "6" , " 1 " , "7" ,"1"]
]
Output : The sudoku i s i n v a l i d
Explanation : Repeated " 1 " in the l a s t row

2024
Universidad Industrial de Santander E3T Programación de Computadores II

5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9

Figure 2: Valid sudoku test 1

5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 1

Figure 3: Invalid sudoku test 2

21
Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according
to the following rules: Each row must contain the digits 1-9 without repetition. Each column must
contain the digits 1-9 without repetition. Each of the nine 3 x 3 sub-boxes of the grid must contain
the digits 1-9 without repetition. Note: A Sudoku board (partially filled) could be valid but is not
necessarily solvable. Only the filled cells need to be validated according to the mentioned rules.
Test:
Input : Check Figure 3
board =
[[ "5" , "3" , " . " , " . " , "7" , " . " ," ." ," ." ," ."]
, [ "6" , " . " , " . " , " 1 " , "9" , " 5 " ,". " ,". " ,". "]
, [ " . " , "9" , "8" , " . " , " . " , " . " ,". " , "6" ,". "]
, [ "8" , " . " , " . " , " . " , "6" , " . " ,". " ,". " , "3" ]
, [ "4" , " . " , " . " , "8" , " . " , "3" ,". " ,". " ,"1"]
,[ "7" , " . " , " . " , " . " , "2" , " . " ,". " ,". " , "6" ]
, [ " . " , "6" , " . " , " . " , " . " , " . " , "2" , "8" ,". "]
, [ " . " , " . " , " . " , "4" , " 1 " , "9" ,". " ,". " , "5" ]
,[ " . " , " . " , " . " , " . " , "8" , " . " ,". " , "7" , "9" ] ]

Output : The sudoku i s v a l i d

Input : Check Figure 4


board =
[ [ "8" , "3" , " . " , " . " , "7" , " . " ," ." ," ." ," ."]
, [ "6" , " . " , " . " , " 1 " , "9" , " 5 " ,". " ,". " ,". "]
, [ " . " , "9" , "8" , " . " , " . " , " . " ,". " , "6" ,". "]
, [ "8" , " . " , " . " , " . " , "6" , " . " ,". " ,". " , "3" ]
, [ "4" , " . " , " . " , "8" , " . " , "3" ,". " ,". " ,"1"]
,[ "7" , " . " , " . " , " . " , "2" , " . " ,". " ,". " , "6" ]
, [ " . " , "6" , " . " , " . " , " . " , " . " , "2" , "8" ,". "]
, [ " . " , " . " , " . " , "4" , " 1 " , "9" ,". " ,". " , "5" ]
,[ " . " , " . " , " . " , " . " , "8" , " . " ,". " , "7" , "9" ] ]

Output : The sudoku i s i n v a l i d


Explanation : Since there are two 8 s in the top l e f t 3 x3 sub−box , i t i s i n v a l i d .

2024
Universidad Industrial de Santander E3T Programación de Computadores II

Figure 4: Sudoku test 1

Figure 5: Sudoku test 2

2024

You might also like