Question

In Python: This will require you to write several functions, and then use them in a...

In Python: This will require you to write several functions, and then use them in a program. Logical Calculator The logical calculator does math, but with logical operators. In logic, we represent a bit with 0 as false and a bit with 1 as true. The logical operators are NOT, AND and OR. Bitwise logical calculations operate on each bit of the input. The NOT operator works on just one three-bit argument. NOT 011 = 100 The AND operator works on two three-bit arguments: 011 AND 010 = 010 The OR operator also works on two three-bit arguments: 011 OR 010 = 011 Note that the above are examples to illustrate what a logical calculator does. It is not supposed to be the output of your program. The actual output is below. As you can see, the calculator works bitwise. That is, for an operation that requires two arguments, the first character (bit) of the first string is operated on with the first character (bit) of the second string, and the second character (bit) of the first string is operated on with the second character (bit) of the second string, and so on.

Task 1 Write a function one_bit_NOT that takes one argument, a character that is either '0' or '1'. It should perform the NOT operation and return a string with a single character as the result. I.e., if the character argument is "0", it returns a "1"'. If the chararacter argument is "1", it returns a "0".

Task 2 Write a function three_bit_NOT that takes one argument, a string that has three characters of either '0' or '1', and uses the one_bit_NOT function to calculate the result of applying bitwise logical NOT to the string. It returns a string as a result. For example, three_bit_NOT applied to the string argument "000" should return "111".

Task 3 Write a function one_bit_OR that takes two arguments, a and b, which are strings of single characters of either "0" or "1". It then performs the OR operation on the two chars (a OR b) and returns a string with a single character as the result.

Task 4 Write a function three_bit_OR that takes two arguments. The arguments are two strings that have exactly three characters of either "0"' or "1"', and uses the one_bit_OR function to calculate the result of applying bitwise logical OR to the string. It returns a string as a result.

Task 5 Write a function one_bit_AND that takes two arguments, a and b, which are strings of single characters of either "0" or "1". It then performs the AND operation on the two characters (a AND b) and returns a string with a single character as the result.

Task 6 Write a function three_bit_AND that takes two arguments. The arguments are two strings that have exactly three characters of either 0 or 1, and uses the one_bit_AND function to calculate the result of applying bitwise logical AND to the string. It returns a string as a result.

Homework Answers

Answer #1

Hey Bud, here's your code. Feel free to ask any doubt. Consider giving an upvote :) Thankyou

#!/usr/bin/python3

def one_bit_NOT(bit):
    return str(1^int(bit))

def one_bit_OR(x , y):
    return str(int(x) | int(y))

def one_bit_AND(x , y):
    return str(int(x) & int(y))

def three_bit_OR(x , y):
    final = ""
    for i in range(0,len(x)):
        final += one_bit_OR(x[i] , y[i])
    return final

def three_bit_AND(x , y):
    final = ""
    for i in range(0,len(x)):
        final += one_bit_AND(x[i] , y[i])
    return final

def three_bit_NOT(s):
    final = ""
    for i in range(0,len(s)):
        final += one_bit_NOT(s[i])
    return final

## Tests
print(three_bit_OR("111","001"))
print(three_bit_AND("111","001"))
print(three_bit_NOT("111"))
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
8) Write Python code for a function called occurances that takes two arguments str1 and str2,...
8) Write Python code for a function called occurances that takes two arguments str1 and str2, assumed to be strings, and returns a dictionary where the keys are the characters in str2. For each character key, the value associated with that key must be either the string ‘‘none’’, ‘‘once’’, or ‘‘more than once’’, depending on how many times that character occurs in str1. In other words, the function roughly keeps track of how many times each character in str1 occurs...
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs and compares the character by character. If the two strings are exactly same it returns 0, otherwise it returns the difference between the first two dissimilar characters. You are not allowed to use built-in functions (other than strlen()) for this task. The function prototype is given below: int compare_strings(char * str1, char * str2); Task 3: Test if a string is subset of another...
1. Please write the following in C++ also please show all output code and comment on...
1. Please write the following in C++ also please show all output code and comment on code. 2. Also, use CPPUnitLite to write all test and show outputs for each test. Write CppUnitLite tests to verify correct behavior for all the exercises. The modifications are aimed at making the exercises more conducive to unit tests. Write a function that swaps (exchanges the values of two integers). Use int* as the argument type. Write a second swap function using a reference...
Write a Python function first_chars that takes one parameter, which is a nested list of strings....
Write a Python function first_chars that takes one parameter, which is a nested list of strings. It returns a string containing the first character of all of the non-empty strings (at whatever depth they occur in the nested list) concatenated together. Here are a couple of examples of how the function should behave when you're done: >>> first_chars(['Boo', 'is', 'happy', 'today']) 'Biht' >>>first_chars(['boo', 'was', ['sleeping', 'deeply'], 'that', 'night', ['as', ['usual']]]) 'bwsdtnau'
Write a function called char_counter that counts the number of a certain character in a text...
Write a function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This...
using Python: Write a function named safe input(prompt,type) that works like the Python input function, except...
using Python: Write a function named safe input(prompt,type) that works like the Python input function, except that it only accepts the specified type of input. The function takes two arguments: r prompt: str r type: int, float, str The function will keep prompting for input until correct input of the specified type is entered. The function returns the input. If the input was specified to be a number ( float or int), the value returned will be of the correct...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first,...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first, third, fifth (and so on) characters of the strings, with each character both preceded and followed by the ^ symbol, and with a newline appearing after the last ^ symbol. The function returns no value; its goal is to print its output, but not to return it. Q2) Write a Python function called lines_of_code that takes a Path object as a parameter, which is...
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...
This is for C++ A string is a palindrome if reverse of the string is same...
This is for C++ A string is a palindrome if reverse of the string is same as the original string. For example, “abba” is palindrome, but “abbc” is not palindrome. Basically a string with a plane of symmetry in the middle of it is considered a palindrome. For example, the following strings are palindromic: "abbbbba", "abba", "abbcbba", "a", etc. For this problem, you have an input string consisting of both lowercase or uppercase characters. You are allowed to pick and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT