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

Add files #12

Open
wants to merge 3 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
15 changes: 15 additions & 0 deletions YopaNelly-P2/Ex26/Ex26.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Create a function that will receive two arrays and will return an array with elements that are in the first array
# but not in the second
first_array = [1, 2, 3, 10, 5, 3, 14]
second_array = [-1, 4, 5, 6, 14]


def function(arr1, arr2):
for i in arr1:
for j in arr2:
if i == j:
arr1.remove(i)
return arr1


print(function(first_array, second_array))
16 changes: 16 additions & 0 deletions YopaNelly-P2/Ex27/Ex27.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Create a function that will receive an array of numbers as argument and will return a
# new array with distinct elements


def printDistinct(arr):
for i in arr:
if i not in arr1:
arr1.append(i)
return arr1


arr = [6, 10, 5, 4, 9, 120, 4, 6, 10]
arr1 = []
print(printDistinct(arr))


22 changes: 22 additions & 0 deletions YopaNelly-P2/Ex28/Ex28.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Calculate the sum of first 100 prime numbers and return them in an array
def isprime(n):
if n < 2:
return False
if n == 2:
return True
for p in range(2, (n - 1)):
if n % p == 0:
return False
return True


arr2 = []
for i in range(2, 101):
if isprime(i):
arr2.append(i)
print("The prime numbers are: ", arr2)
sum = 0
for i in range(2, 101):
if isprime(i):
sum += i
print("The sum of the prime numbers is: ", sum)
23 changes: 23 additions & 0 deletions YopaNelly-P2/Ex29/Ex29.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Print the distance between the first 100 prime numbers


def isprime(n):
if n < 2:
return False
if n == 2:
return True
for i in range(2, (n - 1)):
if n % i == 0:
return False
return True


arr = []
arr1 = []
for i in range(1, 101):
if isprime(i):
arr.append(i)
print("The prime numbers are: ", arr)
for i in range(0, len(arr)-1):
arr1.append(arr[i+1] - arr[i])
print("The distance between the prime numbers are: ", arr1)
15 changes: 15 additions & 0 deletions YopaNelly-P2/Ex30/Ex30.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Create a function that will add two positive numbers of indefinite size. The numbers
# are received as strings and the result should be also provided as string.
num1 = input("enter your number: ")
num2 = input("enter an other number: ")


def function(a, b):
if int(a) >= 0 and int(b) >= 0:
sum = int(a) + int(b)
return str(sum)
else:
return "invalid numbers"


print(function(num1, num2))
10 changes: 10 additions & 0 deletions YopaNelly-P2/Ex31/Ex31.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Create a function that will return the number of words in a text
me = "Hello! I am Nelly Come join me let write codes"


def numberOfWords(string):
arr = string.split()
return len(arr)


print("Number of words in the text: ", numberOfWords(me))
10 changes: 10 additions & 0 deletions YopaNelly-P2/Ex32/Ex32.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Create a function that will capitalize the first letter of each word in a text
me = "create a function that will capitalize the first letter of each word in a text"


def capitalizeFirst(string):
return string.title() # Or " ".join(word[0].upper()+word[1:] for word in string.split(" "))


print(capitalizeFirst(me))

11 changes: 11 additions & 0 deletions YopaNelly-P2/Ex33/Ex33.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def function(Yo):
sum = 0
Yo = Yo.split(",")
print(Yo)
for i in Yo:
sum += int(i)
return sum


Yo = "56,24,45,2574,1445,556"
print("The sum is: ", function(Yo))
10 changes: 10 additions & 0 deletions YopaNelly-P2/Ex34/Ex34.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Create a function that returns an array with words inside a text.
text = "hello Create a function that returns an array with words inside a text."


def function(text):
array = text.split()
return array


print(function(text))
12 changes: 12 additions & 0 deletions YopaNelly-P2/Ex35/Ex35.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Create a function to convert a CSV text to a “bi-dimensional” array
def function(string):
arr = []
n = 0
string = string.split()
for i in string:
arr.insert(n, list(i))
n += 1
return arr


print(function("Hello convert csv to array"))
12 changes: 12 additions & 0 deletions YopaNelly-P2/Ex36/Ex36.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Create a function that converts a string to an array of characters
string = "yopa Nelly"
arr = []


def function(string):
for i in string:
arr.append(i)
return arr


print(function(string))
14 changes: 14 additions & 0 deletions YopaNelly-P2/Ex37/Ex37.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Create a function that will convert a string in an array containing the ASCII codes of
# each character
arr = []


def function(string):
string = list(string)
for i in string:
arr.append(ord(i))
return arr


string = "Yo guys have fun :)"
print(function(string))
14 changes: 14 additions & 0 deletions YopaNelly-P2/Ex38/Ex38.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Create a function that will
# convert an array containing ASCII codes in a string

def function(arr):
n = 0
for i in arr:
arr[n] = chr(i)
n += 1
arr = ''.join(arr)
return arr


code = [73, 32, 108, 111, 118, 101, 32, 109, 121, 32, 115, 101, 108, 102]
print(function(code))
25 changes: 25 additions & 0 deletions YopaNelly-P2/Ex39/Ex39.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Implement the Caesar cypher
import string
alphabet = string.ascii_lowercase # a-z


def decrypt():
encrypted_message = input("Enter the message you would like to decrypt: ").strip()
key = int(input("Enter key to decrypt: "))

decrypted_message = ""

for i in encrypted_message:

if i in alphabet:
position = alphabet.find(i)
new_position = (position - key) % 26
new_character = alphabet[new_position]
decrypted_message += new_character
else:
decrypted_message += i
print("Your decrypted message is:\n", decrypted_message )



decrypt()
20 changes: 20 additions & 0 deletions YopaNelly-P2/Ex40/Ex40.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Implement the bubble sort algorithm for an array of numbers


def bubbleSort(arr):
n = len(arr)
swapped = False
for i in range(n - 1):
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
swapped = True
arr[j], arr[j + 1] = arr[j + 1], arr[j]

if not swapped:
return arr


arr = [64, 34, 25, 13, 22, 15, 1, 70]

bubbleSort(arr)
print("Sorted array is:", bubbleSort(arr))
13 changes: 13 additions & 0 deletions YopaNelly-P2/Ex41/Ex41.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Create a function to calculate the distance between two points defined by their x, y
# coordinates
import math


def Distance(P1, P2):
distance = math.sqrt(((p1[0] - p2[0]) ** 2) + ((p1[1] - p2[1]) ** 2))
return distance


p1 = [4, 6]
p2 = [5, 5]
print("Distance between the two point is: ", Distance(p1, p2))
13 changes: 13 additions & 0 deletions YopaNelly-P2/Ex42/Ex42.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import math
# Create a function that will return a Boolean value indicating if two circles
# defined by center coordinates and radius are intersecting
import math


# Function to check if two circles touch each other
def function(x1, y1, x2, y2, r1, r2):
d = math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))
return d <= r1+r2


print(function(200, 200, 100, 300, 300, 50))
14 changes: 14 additions & 0 deletions YopaNelly-P2/Ex43/Ex43.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Create a function that will receive a bi-dimensional array as argument and a
# number and will extract as a unidimensional array the column specified by the
# number
from array import array


def function(arr, n):
arr = [[0] * 10] * 8
arr[0][2] = 1
return arr[n]


arr = []
print(function(arr, 1))
20 changes: 20 additions & 0 deletions YopaNelly-P2/Ex44/Ex44.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Create a function that will convert a string containing a binary number into a
# number


from math import pow
arr = []


def function(string):
for i in string:
arr.append(i)
n = len(arr)
sum = 0
for num in arr:
sum += int(num)*pow(2, n-1)
n -= 1
return round(sum)


print(function("111"))
15 changes: 15 additions & 0 deletions YopaNelly-P2/Ex45/Ex45.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Create a function to calculate the sum of all the numbers in a jagged array
# (contains numbers or other arrays of numbers on an unlimited number of
# levels)


def sumjegged(arr):
sum = 0
for num in arr:
if type(num) == list:
num = sumjegged(num)
sum += num
return sum


print(sumjegged([[7, 2], [68, 45, [23]], [10, 45], 1, 4]))
12 changes: 12 additions & 0 deletions YopaNelly-P2/Ex46/Ex46.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Find the maximum number in a jagged array of numbers or array of numbers
def function(arr):
max = arr[0]
for num in arr:
if type(num) == list:
num = function(num)
if max < num:
max = num
return max


print(function([2, 4, 10, [12, 4, [100, 99], 4], [3, 2, 99], 0]))
18 changes: 18 additions & 0 deletions YopaNelly-P2/Ex47/Ex47.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Deep copy a jagged array
# with numbers or other arrays in a new array
arr1 = [2, 4, 10, [12, 4, [100, 99], 4], [3, 2, 99], 0]
# arr2 = arr1.copy()
# print(arr2)


def copyArray(arr):
arr2 = []
for num in arr1:
if num == list:
num = copyArray(num)
arr2.append(num)
return arr2


print(copyArray(arr1))

15 changes: 15 additions & 0 deletions YopaNelly-P2/Ex48/Ex48.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Create a function to return the longest word(s) in a string
text = "Create a function to return the longestn word(s) in a string"
text = text.split()
arr2 = []


def longustWord(text):
longustWord = ""
for word in text:
if len(word) > len(longustWord):
longustWord = word
return longustWord


print(longustWord(text))
14 changes: 14 additions & 0 deletions YopaNelly-P2/Ex49/Ex49.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Shuffle an array of strings
import random


def function(string):
arr = list(string)
random.shuffle(arr)
result = ''.join(arr)
return result


string = "Shuffle an array of strings"
print(function(string))

Loading