Question
I have never learned Python before and we have not touched on this in class yet! Please help! 1) Consider the Python list A =
I have never learned Python before and we have not touched on this in class yet! Please help!
1) Consider the Python list A = [4, 8, 15, 16, 23, 42].
Let D(i,n)D(i,n) be the propositional function "element ii is divisible by nn", where the domain for ii is the possible indices within the given Python list A, and the domain for nn is all positive integers.
Note that a number pp is divisible by another number qq if p/qp/q equals some integer.
Write a Python function D(i,n) that takes as input ii and nn, and returns a single logical value (True or False) representing the truth value of D(i,n)D(i,n).
For example:
D(0,2) = True because A[0] = 4 which is divisible by 2
D(1,3) = False because A[1] = 8 which is not divisible by 3
Some notes:
The first argument of D(i,n) should be the index within the list A, and the second argument should be the number to check divisibility by.
The test cases will only consider indices within the list (that is, 0i50i5).
For example:
Test | Result |
---|---|
print(D(0, 2)) | True |
print(D(1, 3)) | False |
print(D(2, 3)) | True |
2)
Write a Python function ABCs that takes in two lists of the same length, letters and numbers, and returns a single logical value (True or False) representing the truth value of the following proposition. Read the notes below before starting to write code.
x (even(x)vowel(x))x (even(x)vowel(x))
The domain for xx is all indices within the letters and numbers lists. For example:
even(1) = T if [1]numbers[1] is even
even(1) = F if [1]numbers[1] is odd
vowel(1) = T if [1]letters[1] is a vowel
vowel(1) = F if [1]letters[1] is a consonant
Note 1: You can copy-paste the code declaring letters and numbers for the various visible test cases below. We strongly encourage you to do this to test your code. Note 2: You may assume that:
the numbers list will consist of only positive integers (1, 2, 3, ...);
the letters list will consist of only capital English letters as character strings ('A', 'B', 'C', ...);
the first argument of ABCs() should be letters and the second argument should be numbers;
letters and numbers will be the same length;
and that length will be at least 1 (no empty lists).
Note 3/Hint: You can check if a variable xx is in a Python list AA by using: "x in A". And a useful list to define might be: vowels = ['A','E','I','O','U']
For example:
Test | Result |
---|---|
letters = ['A','B','C'] numbers = [ 1 , 2 , 3 ] print(ABCs(letters, numbers)) | False |
letters = ['A','A','A','A'] numbers = [ 1 , 2 , 3 , 4 ] print(ABCs(letters, numbers)) | True |
letters = ['Z','A','I','I', 'B' , 'U'] numbers = [ 1 , 20, 4 , 16, 123 , 100] print(ABCs(letters, numbers)) | True |
for number 2 this is what I started on my own:
def ABCs(letters, numbers): for i in range(len(numbers)): for j in range(len(numbers[i])): if (numbers[i][j] == 'even'): if (letters[i][j] != 'vowel'): return False return True def main(): letters = [['A' , 'B', 'C']] numbers = [[1, 2, 3]] print(ABCs(letters, numbers)) main()
3)
Write a function _check_proposition that takes as its sole argument a Python list of between 0 and 50 integers and returns a boolean (i.e. True or False) representing the truth value of the the proposition x[E(x)y(x=2y)]x[E(x)y(x=2y)], where here the proposition E(x)E(x) means "xx is even" and the list of integers is the domain for xx and yy.
For example:
Test | Result |
---|---|
print(check_proposition([-2,-1,0,1,2,3])) | True |
print(check_proposition([-2,-1,0,1,3,4])) | False |
This is what I have:
def check_proposition(element_lists): for k in range(len(element_lists)): if element_lists[k]%2 == 0: status = False for n in range(len(element_lists)): if element_lists[n]*2 == element_lists[k]: status = True break if status == False: return False return True print(check_proposition([-2, -1, 0, 1, 2, 3])) print(check_proposition([-2, -1, 0, 1, 3, 4]))
Thank you!!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started