I was given an assignment to write three sections of Code.
1. Look up password
2. Decrypt a password.
3. What website is this password for?
Here's the base code:
import csv
import sys
#The password list - We start with it populated for testing purposes
passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"]]
#The password file name to store the passwords to
passwordFileName = "samplePasswordFile"
#The encryption key for the caesar cypher
encryptionKey=16
#Caesar Cypher Encryption
def passwordEncrypt (unencryptedMessage, key):
#We will start with an empty string as our encryptedMessage
encryptedMessage = ''
#For each symbol in the unencryptedMessage we will add an encrypted symbol into the encryptedMessage
for symbol in unencryptedMessage:
if symbol.isalpha():
num = ord(symbol)
num += key
if symbol.isupper():
if num > ord('Z'):
num -= 26
elif num < ord('A'):
num += 26
elif symbol.islower():
if num > ord('z'):
num -= 26
elif num < ord('a'):
num += 26
encryptedMessage += chr(num)
else:
encryptedMessage += symbol
return encryptedMessage
def loadPasswordFile(fileName):
with open(fileName, newline='') as csvfile:
passwordreader = csv.reader(csvfile)
passwordList = list(passwordreader)
return passwordList
def savePasswordFile(passwordList, fileName):
with open(fileName, 'w+', newline='') as csvfile:
passwordwriter = csv.writer(csvfile)
passwordwriter.writerows(passwordList)
while True:
print("What would you like to do:")
print(" 1. Open password file")
print(" 2. Lookup a password")
print(" 3. Add a password")
print(" 4. Save password file")
print(" 5. Print the encrypted password list (for testing)")
print(" 6. Quit program")
print("Please enter a number (1-4)")
choice = input()
if(choice == '1'): #Load the password list from a file
passwords = loadPasswordFile(passwordFileName)
if(choice == '2'): #Lookup at password
print("Which website do you want to lookup the password for?")
for keyvalue in passwords:
print(keyvalue[0])
passwordToLookup = input()
####### YOUR CODE HERE ######
#You will need to find the password that matches the website
#You will then need to decrypt the password
#
#1. Create a loop that goes through each item in the password list
# You can consult the reading on lists in Week 5 for ways to loop through a list
#
#2. Check if the name is found. To index a list of lists you use 2 square backet sets
# So passwords[0][1] would mean for the first item in the list get it's 2nd item (remember, lists start at 0)
# So this would be 'XqffoZeo' in the password list given what is predefined at the top of the page.
# If you created a loop using the syntax described in step 1, then i is your 'iterator' in the list so you
# will want to use i in your first set of brackets.
#
#3. If the name is found then decrypt it. Decrypting is that exact reverse operation from encrypting. Take a look at the
# caesar cypher lecture as a reference. You do not need to write your own decryption function, you can reuse passwordEncrypt
#
# Write the above one step at a time. By this I mean, write step 1... but in your loop print out every item in the list
# for testing purposes. Then write step 2, and print out the password but not decrypted. Then write step 3. This way
# you can test easily along the way.
#
####### YOUR CODE HERE ######
if(choice == '3'):
print("What website is this password for?")
website = input()
print("What is the password?")
unencryptedPassword = input()
####### YOUR CODE HERE ######
#You will need to encrypt the password and store it in the list of passwords
#The encryption function is already written for you
#Step 1: You can say encryptedPassword = passwordEncrypt(unencryptedPassword,encryptionKey)]
#the encryptionKey variable is defined already as 16, don't change this
#Step 2: create a list of size 2, first item the website name and the second item the password.
#Step 3: append the list from Step 2 to the password list
####### YOUR CODE HERE ######
if(choice == '4'): #Save the passwords to a file
savePasswordFile(passwords,passwordFileName)
if(choice == '5'): #print out the password list
for keyvalue in passwords:
print(', '.join(keyvalue))
if(choice == '6'): #quit our program
sys.exit()
print()
print()
In: Computer Science
In: Accounting
this is my linkedlist and to insert a new element in it , however i want it to be user entered linkedlist
|
#include <bits/stdc++.h> using namespace std;
// A linked list Node struct Node { int data; struct Node* next; };
// Size of linked list int size = 0;
// function to create and return a Node Node* getNode(int data) { // allocating space Node* newNode = new Node();
// inserting the required data newNode->data = data; newNode->next = NULL; return newNode; }
// function to insert a Node at required position void insertPos(Node** current, int pos, int data) { // This condition to check whether the // position given is valid or not. if (pos < 1 || pos > size + 1) cout << "Invalid position!" << endl; else {
// Keep looping until the pos is zero while (pos--) {
if (pos == 0) {
// adding Node at required position Node* temp = getNode(data);
// Making the new Node to point to // the old Node at the same position temp->next = *current;
// Changing the pointer of the Node previous // to the old Node to point to the new Node *current = temp; } else // Assign double pointer variable to point to the // pointer pointing to the address of next Node current = &(*current)->next; } size++; } }
// This function prints contents // of the linked list void printList(struct Node* head) { while (head != NULL) { cout << " " << head->data; head = head->next; } cout << endl; }
// Driver Code int main() { // Creating the list 3->5->8->10 Node* head = NULL; head = getNode(3); head->next = getNode(5); head->next->next = getNode(8); head->next->next->next = getNode(10);
size = 4;
cout << "Linked list before insertion: "; printList(head);
int data = 12, pos = 3; insertPos(&head, pos, data); cout << "Linked list after insertion of 12 at position 3: "; printList(head);
// front of the linked list data = 1, pos = 1; insertPos(&head, pos, data); cout << "Linked list after insertion of 1 at position 1: "; printList(head);
// insetion at end of the linked list data = 15, pos = 7; insertPos(&head, pos, data); cout << "Linked list after insertion of 15 at position 7: "; printList(head);
return 0; } |
In: Computer Science
JAVA
Start with the SelectionSort class in the zip file attached to this item. Keep the name SelectionSort, and add a main method to it.
In your submission write some text describing the relationship between the number of comparisons of the various values of NUM_ELEMENTS. For example, what do we find if we divide the number of comparisons for 2000 elements by the number of comparisons for 1000 elements? What do we find if we divide the number of comparisons for 4000 elements by the number of comparisons for 2000 elements?
SELECTION SORT FILE MUST USE THIS IN PROGRAM!!! PLEASE
public class SelectionSort {
/** The method for sorting the numbers */
public static void selectionSort(double[] list) {
for (int i = 0; i < list.length - 1; i++) {
// Find the minimum in the list[i..list.length-1]
double currentMin = list[i];
int currentMinIndex = i;
for (int j = i + 1; j < list.length; j++) {
if (currentMin > list[j]) {
currentMin = list[j];
currentMinIndex = j;
}
}
// Swap list[i] with list[currentMinIndex] if
necessary;
if (currentMinIndex != i) {
list[currentMinIndex] = list[i];
list[i] = currentMin;
}
}
}
}
In: Computer Science
9.8 LAB: Finding the first and last occurrence of a value (doubly-linked list)
Given main() and a PeopleNode class, complete the PeopleList class by writing findFirst() and findLast() methods. The findFirst() method should find the first occurrence of an age value in the linked list and return the corresponding node. Similarly, the findLast() method should find the last occurrence of the age value in the linked list and return the corresponding node. For both methods, if the age value is not found, null should be returned. The program will replace the name value of each found node with a new name.
Ex. If the input is
Alex 23 Tom 41 Michelle 34 Vicky 23 -1 23 Connor 34 Michela
the output is
List before replacing: Alex, 23 Tom, 41 Michelle, 34 Vicky, 23 List after replacing the first occurrence of 23: Connor, 23 Tom, 41 Michelle, 34 Vicky, 23 List after replacing the last occurrence of 34: Connor, 23 Tom, 41 Michela, 34 Vicky, 23
FindOccurence.java
import java.util.Scanner;
public class FindOccurrence {
public static void main (String[] args) {
Scanner scnr = new Scanner(System.in);
PersonList personList = new PersonList();
PersonNode curNode;
PersonNode foundFirst;
PersonNode foundLast;
String name;
String replaceName;
int age;
int findAge;
name = scnr.next();
while (!name.equals("-1")) {
// Insert into linked list
age = scnr.nextInt();
curNode = new PersonNode(name, age);
personList.append(curNode);
name = scnr.next();
}
System.out.println("List before replacing:");
personList.printPersonList();
foundFirst = new PersonNode();
findAge = scnr.nextInt();
foundFirst = personList.findFirst(findAge);
if (foundFirst != null) {
replaceName = scnr.next();
foundFirst.setName(replaceName);
System.out.println();
System.out.println("List after replacing the first occurrence of "
+ findAge + ":");
personList.printPersonList();
}
else {
System.out.println("Age value not found in the list.");
}
foundLast = new PersonNode();
findAge = scnr.nextInt();
foundLast = personList.findLast(findAge);
if (foundLast != null) {
replaceName = scnr.next();
foundLast.setName(replaceName);
System.out.println();
System.out.println("List after replacing the last occurrence of " +
findAge + ":");
personList.printPersonList();
}
else {
System.out.println("Age value not found in the list.");
}
}
}
PersonList.java
public class PersonList {
// Linked list nodes
public PersonNode headNode;
public PersonNode tailNode;
public PersonList() {
// Front of nodes list
headNode = null;
tailNode = null;
}
// append
public void append(PersonNode newNode) {
if (headNode == null) { // List empty
headNode = newNode;
tailNode = newNode;
}
else {
tailNode.nextNode = newNode;
newNode.prevNode = tailNode;
tailNode = newNode;
}
}
// prepend
public void prepend(PersonNode newNode) {
if (headNode == null) { // list empty
headNode = newNode;
tailNode = newNode;
}
else {
newNode.nextNode = headNode;
headNode.prevNode = newNode;
headNode = newNode;
}
}
// insertAfter
public void insertAfter(PersonNode curNode, PersonNode newNode)
{
PersonNode sucNode;
if (headNode == null) { // List empty
headNode = newNode;
tailNode = newNode;
}
else if (curNode == tailNode) { // Insert after tail
tailNode.nextNode = newNode;
newNode.prevNode = tailNode;
tailNode = newNode;
}
else {
sucNode = curNode.nextNode;
newNode.nextNode = sucNode;
newNode.prevNode = curNode;
curNode.nextNode = newNode;
sucNode.prevNode = newNode;
}
}
// TODO: Write findFirst() method
// Find the node with the first occurrence of the age
// Start with the headNode and traverse forward
public PersonNode findFirst(int ageValue) {
}
// TODO: Write findLast() method
// Find the node with the last occurrence of the age
// Start with the tailNode and traverse backward
public PersonNode findLast(int ageValue) {
}
public void printPersonList() {
PersonNode curNode;
curNode = headNode;
while (curNode != null) {
curNode.printNodeData();
curNode = curNode.nextNode;
}
}
}
PersonNode.java
public class PersonNode {
private String name;
private int age;
public PersonNode prevNode; // Reference to the previous node
public PersonNode nextNode; // Reference to the next node
public PersonNode() {
name = "";
age = 0;
prevNode = null;
nextNode = null;
}
// Constructor
public PersonNode(String nameInit, int ageInit) {
this.name = nameInit;
this.age = ageInit;
this.prevNode = null;
this.nextNode = null;
}
// Constructor
public PersonNode(String nameInit, int ageInit, PersonNode
prevNode, PersonNode newNextNode) {
this.name = nameInit;
this.age = ageInit;
this.prevNode = prevNode;
this.nextNode = newNextNode;
}
public String getName() {
return this.name;
}
public long getAge() {
return this.age;
}
public void setName(String userName) {
this.name = userName;
}
public void setAge(int userAge) {
this.age = userAge;
}
public void printNodeData() {
System.out.println(this.name + ", " + this.age);
}
}
In: Computer Science
Question
Objective:
The objective of this lab exercise is to give you practice in programming with one of Python’s most widely used “container” data types -- the List (commonly called an “Array” in most other programming languages). More specifically you will demonstrate how to:
Discussion:
For this exercise you will be simulating a Weather Station responsible for recording hourly temperatures and reporting on certain characteristics (e.g., average temperature) from your recordings. You will also be expanding on your experience in creating functions and passing them more complex parameters (i.e., complete list arguments – sometimes empty and sometimes full).
Specifications:
DDI&T a Python program to input, store, and process hourly temperatures for each hour of the day (i.e., 24 temperatures). Your program should be divided logically into the following parts:
HourlyTemperatures = []
GetTemperatures(HourlyTemperatures)
This function must interactively prompt for and input temperatures for each of the 24 hours in a day (0 through 23). For each temperature that is input, verify that its value lies in the range of minus 50 degrees and plus 130 degrees (i.e., validate your input values). If any value is outside the acceptable range, ask the user to re-enter the value until it is within this range before going on to the next temperature (HINT: use a nested loop (e.g., Python’s equivalent to a do-while loop) that exits only when a value in the specified range has been entered). Store each validated temperature in its corresponding element of the list container passed as a parameter to the function (Hint: Look at the ‘append(…)’ function that can be called on a list container).
ComputeAverageTemp(HourlyTemperatures)
This function computes the average temperature of all the temperatures in the list and returns this average to the calling function.
DisplayTemperatures(HourlyTemperatures, AverageTemp)
which displays the values of your temperature list in a columnar format followed by the values for the high temperature, low temperature, and average temperature for the day. NOTE: If you want to create separate function(s) to find and return the high and low temperature values, then feel free to do so!
The resulting output should look something like this:
Hour Temperature
00:00 42
01:00 42
. . . . . . . . // Your program output must include all of these too!
22:00 46
23:00 48
High Temperature: 68
Low Temperature: 42
Average Temperature: 57.4
5. Since you have several created functions to perform each of the major steps of your solution, your main(): function should be quite simple. The pseudo code for main() might look something like this:
main()
#declare any necessary variable(s) and HourlyTemperatures list
while user-wants-to-process-another-days’-worth-of-temperatures
call GetTemperatures(HourlyTemperatures)
call ComputeAverageTemp(HourlyTemperatures)
call DisplayTemperatures(HourlyTemperatures, AverageTemperature)
ask if user want to process another days’ worth of temperatures
6. Test your program with at least two (2) different sets of temperatures. Make sure you enter values to adequately test your temperature-validation code (e.g., temperatures below –50 and above +130 degrees).
Deliverable(s):
Your deliverable should be a Word document with screenshots showing the sample code you have created, and discuss the issues that you had for this project related to AWS and/or Python IDE and how you solved them.
Turn in the properly documented source listing of your program and complete outputs from at least TWO (2) test “runs”. Additionally, turn in screen captures of some of your interactive inputs demonstrating that your program properly detects invalid inputs and prompts the user to re-enter the temperature(s).
In: Computer Science
In this assignment you are to utilize the Node data
structure provided on Blackboard.
In this assignment you are to write a main program that implements
two methods and a main
method as their driver. So, only main and two methods with it.
Implementation Details:
Method 1:
^^^^^^^^^
Parameters and return type:
Takes as parameters an array of integers, the size of the array of
integer (.length is acceptable also)
and it should return a Node that is supposed to represent the head
of a linked list.
Note: You may also choose to make it a void method and
pass the head of the linked list as a parameter,
if that's easier for you. You have the choice here.
Method Logic:
The method is supposed to create a linked list represented by its
head and populate it with the
numbers stored in the array by placing the even numbers in the
array first, followed by the odd
numbers. You may not change the order of the numbers inside of the
array, the order must remain
the same as it was read from input.
Example: Assume the array is: [0,1, 4, 6, 7, 9, 2, 10, 11, 14, 13, 19, 20]
The linked list should be 20->14->10->2->6->4->0->1->7->9->11-13->19
So return back from the function the head of this
linked list.
Method 2:
^^^^^^^^^
Parameters and return type:
This method should take as a parameter the linked list generated in
method 1 represented by
its head.
Method logic:
The method should start by reading in one integer from standard
input, and based on that
integer, it needs to shift the linked list by that many positions.
Keep in mind that you need
to do the necessary error checking, the shifting can be between 0
and the size of the linked list - 1.
Example:
Assume the given linked list is:
20->14->10->2->6->4->0->1->7->9->11-13->19
You read in an integer: You input the number 3.
The linked list should look like:
2->6->4->0->1->7->9->11-13->19->20->14->10
If you read in a 6:
The linked list should look like:
0->1->7->9->11-13->19->20->14->10->2->6->4
If you read in a 13 The method should print an error
asking you to enter a number between 0-12.
The main program:
^^^^^^^^^^^^^^^^^
1. Your program should run and ask the user to input the size of an
array of integers. Once that's
done, the program should read these integers and store them into an
array.
2. Once the array has been populated and its size is
known, then you need to call method 1 defined
above. The result should be a head pointer of a linked list.
At this point you declare a cursor method and go through the linked
list and print it to the screen.
Based on the above example: 20 14 10 2 6 4 0 1 7 9 11 13 19.
3. Call method 2
4. Print the linked list resulting from calling method 2. The rotated linked list.
- method 1 hints
The following should happen after reading an array a of size n Keep
in mind that this is closer to pseudocode, so you need to fix it to
compile, it's pretty close to what you need to do, but you need to
adjust the code to work with the Node data structure. I am talking
about private vs. public data members and the use of getters and
setters.
Also the following is assuming a dummy node.
Node head = new Node(); Node cursor = head;
for(i = 0; i < n; i++ ) {
int x = a[i]; // this is the number we're working with.
if(x % 2 == 0) // the number is even.
{
// this will insert the even numbers in the reverse order of how
they are in the array.
head.next = new Node(x, head.next);
}else{
cursor.next = new Node(x, null);
cursor = cursor.next
}
}
return head; // this will return the entire list represented by its
head
- Method 2 hints
public static void rotateList(Node head){
// read in the number n to rotate by
// then do the rotation code by manipulating the head and where
it's
// pointing and where the new end of the list is
now.
}
In: Computer Science
In this assignment you are to utilize the Node data
structure provided on Blackboard.
In this assignment you are to write a main program that implements
two methods and a main
method as their driver. So, only main and two methods with it.
Implementation Details:
Method 1:
^^^^^^^^^
Parameters and return type:
Takes as parameters an array of integers, the size of the array of
integer (.length is acceptable also)
and it should return a Node that is supposed to represent the head
of a linked list.
Note: You may also choose to make it a void method and
pass the head of the linked list as a parameter,
if that's easier for you. You have the choice here.
Method Logic:
The method is supposed to create a linked list represented by its
head and populate it with the
numbers stored in the array by placing the even numbers in the
array first, followed by the odd
numbers. You may not change the order of the numbers inside of the
array, the order must remain
the same as it was read from input.
Example: Assume the array is: [0,1, 4, 6, 7, 9, 2, 10, 11, 14, 13, 19, 20]
The linked list should be 20->14->10->2->6->4->0->1->7->9->11-13->19
So return back from the function the head of this
linked list.
Method 2:
^^^^^^^^^
Parameters and return type:
This method should take as a parameter the linked list generated in
method 1 represented by
its head.
Method logic:
The method should start by reading in one integer from standard
input, and based on that
integer, it needs to shift the linked list by that many positions.
Keep in mind that you need
to do the necessary error checking, the shifting can be between 0
and the size of the linked list - 1.
Example:
Assume the given linked list is:
20->14->10->2->6->4->0->1->7->9->11-13->19
You read in an integer: You input the number 3.
The linked list should look like:
2->6->4->0->1->7->9->11-13->19->20->14->10
If you read in a 6:
The linked list should look like:
0->1->7->9->11-13->19->20->14->10->2->6->4
If you read in a 13 The method should print an error
asking you to enter a number between 0-12.
The main program:
^^^^^^^^^^^^^^^^^
1. Your program should run and ask the user to input the size of an
array of integers. Once that's
done, the program should read these integers and store them into an
array.
2. Once the array has been populated and its size is
known, then you need to call method 1 defined
above. The result should be a head pointer of a linked list.
At this point you declare a cursor method and go through the linked
list and print it to the screen.
Based on the above example: 20 14 10 2 6 4 0 1 7 9 11 13 19.
3. Call method 2
4. Print the linked list resulting from calling method 2. The rotated linked list.
- method 1 hints
The following should happen after reading an array a of size n Keep
in mind that this is closer to pseudocode, so you need to fix it to
compile, it's pretty close to what you need to do, but you need to
adjust the code to work with the Node data structure. I am talking
about private vs. public data members and the use of getters and
setters.
Also the following is assuming a dummy node.
Node head = new Node(); Node cursor = head;
for(i = 0; i < n; i++ ) {
int x = a[i]; // this is the number we're working with.
if(x % 2 == 0) // the number is even.
{
// this will insert the even numbers in the reverse order of how
they are in the array.
head.next = new Node(x, head.next);
}else{
cursor.next = new Node(x, null);
cursor = cursor.next
}
}
return head; // this will return the entire list represented by its
head
- Method 2 hints
public static void rotateList(Node head){
// read in the number n to rotate by
// then do the rotation code by manipulating the head and where
it's
// pointing and where the new end of the list is
now.
}
In: Computer Science

The switch in the figure below is open for t<0 and is then thrown closed at time t = 0. Assume R = 8.00 2, L = 8.00 H, and E = 15.0 V. Find the following as functions of time thereafter. Assume current is in A and time is in s. Use the following as necessary: t.)
(a) the current in the inductor
(b) the current in the switch
In: Physics
Consider three ideas (v,c), where idea 1 is (5,25), idea 2 is (4,15), and idea 3 is (6, 29).
Find a value of R such that:
a. Idea 1 is chosen.
b. Idea 2 is chosen.
c. Idea 3 is chosen.
If such a value R does not exist for any of the above, briefly explain why.
In: Accounting