We learned that when calling subprograms or functions that we can pass data to the subprogram or function by ‘value’ or by ‘reference’. Describe what each approach is, how it works and what the potential security disadvantage is when passing parameters by reference.
In: Computer Science
The credit plan at TidBit Computer Store specifies a 10% down payment and an annual interest rate of 12%. Monthly payments are 5% of the listed purchase price, minus the down payment.
Write a program that takes the purchase price as input. The program should display a table, with appropriate headers, of a payment schedule for the lifetime of the loan. Each row of the table should contain the following items: The month number (beginning with 1) The current total balance owed The interest owed for that month
The amount of principal owed for that month The payment for that month The balance remaining after payment
The amount of interest for a month is equal to balance × rate / 12. The amount of principal for a month is equal to the monthly payment minus the interest owed.
I am using Python.
In: Computer Science
THIS IS ALL ON PYTHON
Recursion Practice
Recall from lecture that a recursive function is one that calls on itself. Recursive functions have two parts, the base case which stops the recursion and the recursive step where the function calls upon itself.
When designing recursive algorithms, your goal for each step should be to work towards the base case. More abstractly, each recursive call decreases the size of the problem, working ever closer to some trivial case with a defined solution.
uppercount(s)
Write a recursive function uppercount(s) that counts a number of uppercase letters in the input string. Do not use loops or built-in Python string functions (the only string functions you can use are slicing and indexing).
>>>> n = uppercount("Hello, World") print(n) 2
Add comments to your code to point out the base case and the recursive case.
clean_string(s)
From the standpoint of Python, words like ”world” and ”world!” as different words. So if we wrote an automatic English-to-Spanish translator that replaced ”world” with ”mundo”, ”world” would have been replaced and ”world!” wouln’t. I.e. we notice that before we can write a program for automatic translation, we need to find a way to ignore punctuation in a string. Write a recursive function clean_string(s) that would remove all symbols that are not uppercase or lowercase letters or space from s and return the ”clean” string.
Do not use loops.
>>>> CS = clean_string("Hello, world") print(CS) Hello world
Add comments to your code to point out the base case and the recursive case.
clean_list(lst1, lst2)
Using a similar idea to the one you employed in clean string(s) function, write a recursive function clean_list(l1, l2) that takes two lists as input and returns a list of elements from lst1 that are not present in lst2.
Do not use loops.
>>>> unique = clean_list([1,2,3,4,5,6,7], [2,4,6]) print(unique) [1,3,5,7]
Add comments to your code to point out the base case and the recursive case.
Recursions vs. Iteration
As we discussed, recursion is a very powerful problem-solving technique. In fact, any computation can be expressed recursively. However, recursion doesn’t always lead to efficient solution, as you will now see. A classic example of recursion is the definition of Fibonacci number.
Wikipedia has a good article about it: Fibonacci Number. (Remember to check the part that talks about Fibonacci numbers in nature).
A Fibonacci number is defined as follows:
F(0) = 0 F(1) = 1 F(n) = F(n−1) + F(n−2) for n > 1.
Recursive Fibonacci
Write a recursive function F(n) that returns n-th Fibonacci number. In comments, mark the base case and recursive step.
Iterative Fibonacci
Write an iterative function (using iteration, i.e. loop(s)) f(n) that returns n-th Fibonacci number.
In: Computer Science
Suppose that our 9-member MIPS core instruction subset is to be augmented to include the addiu instruction. Explain how just the control bits generated by the control unit must be expanded or modified to allow the addiu instruction to be executed on the single-cycle datapath. Indicate the proper setting for each of the bits generated by the control unit for an addiu instruction
In: Computer Science
I NEED TO DEBUG THIS PSUEDOCODE
// This pseudocode should determine and output the rental fees
for cars.
// Standard cars rent for $65 per day, compacts rent for $40 per
day,
// and subcompacts rent for $30 per day. Rentals for at least 7
days
// receive a 20% discount. An error message is displayed if the car
type
// is not valid.
start
Declarations
string carType
num days
num STD_RATE = 65
num COM_RATE = 40
num SUB_RATE = 30
num DAYS_FOR_DISCOUNT = 10
num DISCOUNT_RATE = 0.20
string QUIT = "ZZZZ"
getReady()
while carType <> QUIT
detailLoop()
endwhile
finish()
stop
getReady()
output Enter car type or , QUIT, to quit
input carType
return
detailLoop()
output "Enter days rented "
input days
if carType = "Standard" then
rate = STD_RATE
else
if car_Type = "Compact" then
rate = COMPACT_RATE
else
if carType = "Subcompact" then
rate = SUB_RATE
else
rate = 0
output "Invalid car type"
endif
endif
endif
if rate <> 0
if days >= DAYS_FOR_DISCOUNT then
rate = rate * DISCOUNT_RATE
endif
output carType, days
output "Enter car type or ", QUIT, " to quit "
input carType
return
finish()
output "End of program"
return
In: Computer Science
Draw a Venn diagram for 4 rectangles with all sides vertical or horizontal. |
In: Computer Science
What features should you be looking for selecting routers, switches, and servers?
In: Computer Science
The Fibonacci sequence 1, 1, 2, 3, 5, 8, 13, 21…… starts with two 1s, and each term afterward is the sum of its two predecessors. Please write a function, Fib(n), which takes n as the input parameter. It will return the n-th number in the Fibonacci sequence. Using R, the output for Fib(9) should give only the 9th element in the sequence and not any of the previous elements. Please Help :)
In: Computer Science
NEED TO DEBUG
// This pseudocode should create a report that contains an
// apartment complex rental agent's commission. The
// program accepts the ID number and name of the agent who
// rented the apartment, and the number of bedrooms in the
// apartment. The commission is $100 for renting a
three-bedroom
// apartment, $75 for renting a two-bedroom apartment, $55
for
// renting a one-bedroom apartment, and $30 for renting a
studio
// (zero-bedroom) apartment. Output is the salesperson’s
// name and ID number and the commission earned on the
rental.
start
Declarations
num salesPersonID
string salesPersonName
num numBedrooms
num COMM_3 = $100.00
num COMM_2 = $75.00
num COMM_1 = $55.00
num COMM_STUDIO = $30.00
num QUIT = 9999
getReady()
while salesPersonID <> QUIT
detailLoop()
endwhile
finish()
stop
getReady()
output "Enter salesperson ID or ", QUIT, " to quit "
output salesperson_ID
return
detailLoop()
output "Enter name "
input salesPersonName
output "Enter number of bedrooms rented "
input numBedrooms
if numBedrooms > 3 then
commissionEarned = COMM_3
else
if numBedrooms < 2 then
commissionEarned = COMM_2
else
if numBedrooms > 1 then
commission = COMM_1
else
commission = COMM_4
endif
endif
endif
output salesPersonID, salesPersName, commissionEarned
output "Enter salesperson ID or ", QUIT, " to quit "
input salesPersonID
return
finish()
output "End of report"
return
In: Computer Science
The program must prompt for an integer divisor in the range 10-20 and will print all numbers 1-1000 that are divisible by that divisor. You MUST use a while loop for this. Use printf to display the numbers in right-aligned columns, 10 to a line. For example, “%5d” would be the format String to allow a field width of 5 to display the number. If the number entered by the user is out of range just print an error message. (Java)
In: Computer Science
write project Queens problems / c language
In: Computer Science
NEED TO DEBUG
// This pseudocode should create a list that describes annual
profit
// statistics for a retail store. Input records contain a
department
// name (for example, “Cosmetics”) and profits for each quarter
for
// the last two years. The program should determine whether
// the profit is higher, lower, or the same
// for this full year compared to the last full year.
start
Declarations
string department
num salesQuarter1ThisYear
num salesQuarter2ThisYear
num salesQuarter3ThisYear
num salesQuarter3ThisYear
num salesQuarter1LastYear
num salesQuarter2LastYear
num salesQuarter3ThisYear
num salesQuarter4LastYear
num totalThisYear
num totalLastYear
string status
num QUIT = "ZZZZ"
housekeeping()
while department <> QUIT
compareProfit()
endwhile
finishUp()
stop
housekeeping()
output "Enter department name or ", QUIT, " to quit "
input dept
return
compareProfit()
getSalesData()
sumSalesData()
if totalThisYear = totalLastYear then
status = "Higher"
else
if totalThisYear <= totalLastYear then
status = "Lower"
else
status = "Same"
endif
endif
output department, status
output "Enter department name or ", QUIT, " to quit "
input department
return
getSalesData()
output "Enter sales for first quarter this year "
input salesQuarter1ThisYear
output "Enter sales for second quarter this year "
input salesQuarter1ThisYear
output "Enter sales for third quarter this year "
input salesQuarter1ThisYear
output "Enter sales for fourth quarter this year "
input salesQuarter4ThisYear
output "Enter sales for first quarter last year "
input salesQuarter1LastYear
output "Enter sales for second quarter last year "
input salesQuarter3LastYear
output "Enter sales for third quarter last year "
input salesQuarter3LastYear
output "Enter sales for fourth quarter last year "
input salesQuarter3LastYear
return
sumSalesData()
totalThisYear = salesQuarter1ThisYear + salesQuarter2ThisYear
+
salesQuarter2ThisYear + salesQuarter4ThisYear
totalLastYear = salesQuarter2LastYear + salesQuarter2LastYear
+
salesQuarter3LastYear + salesQuarter4LastYear
return
finishUp()
output "End of report"
return
In: Computer Science
python
IMPORTANT : For this exercise, you will be defining a
function that USES the Stack ADT. A stack implementation
is provided to you as part of this exercise - you should
not use your own Stack class. Instead, simply use the functions:
Stack(), push(), pop() and is_empty() where necessary inside your
function definition.
For this exercise, you must write a function called
balanced_brackets(). This function will be passed
a string as an input, and you must check that any parentheses or
angled brackets in the string, that is: '(',
'<', ')' and
'>', are correctly balanced.
Here are a few examples of strings where brackets are
correctly balanced:
a(<bcd>ef)g abcde a(b)c<<d>e(fg)>
and here are a few examples where the brackets are not balanced:
ab(cde> a<bc>)def<g> ab)c
Your balanced_brackets() function should return True if the input string is balanced, and False otherwise. Remember, you can assume that an implementation of the Stack ADT is available to you. It is therefore likely that your function definition will begin as follows:
def balanced_brackets(text): s = Stack() ...
For example:
Test | Result |
---|---|
print(balanced_brackets('(<x>)(())()')) |
True |
print(balanced_brackets('x(y)z')) |
True |
In: Computer Science
# Write a function called `get_state_data` that allows you to
specify a state,
# then saves a .csv file (`STATE_data.csv`) with observations from
that state
# This includes data about the state, as well as the counties in
the state
# You should use the full any.drinking dataset in this function
(not just 2012)
# Demonstrate that you function works by passing "Utah" to the
function
state_Utah <- get_state_data(Utah)
############################ Binge drinking Dataset ############################
# In this section, you will ask a variety of questions regarding
the
# `binge_drinking.csv` dataset. More specifically, you will analyze
a subset of
# the observations of *just the counties* (exclude state/national
estimates!).
# You will store your answers in a *named list*, and at the end of
the section,
# Convert that list to a data frame, and write the data frame to a
.csv file.
# Pay close attention to the *names* to be used in the list.
# Create a dataframe with only the county level observations from
the
# `binge_driking.csv` dataset. You should (again) think of
Washington D.C. as
# a state, and therefore *exclude it here*.
# However, you should include "county-like" areas such as parishes
and boroughs
county_data <- binge.drinking.csv %>% distinct(state)
# Create an empty list in which to store answers to the questions below.
# What is the average county level of binge drinking in 2012 for
both sexes?
# Store the number in your list as `avg_both_sexes`.
# What is the name of the county with the largest increase in male
binge
# drinking between 2002 and 2012?
# Store the county name in your list as
`largest_male_increase`.
# How many counties experienced an increase in male binge drinking
between
# 2002 and 2012?
# Store the number in your list as `num_male_increase`.
# What fraction of counties experienced an increase in male binge
drinking
# between 2002 and 2012?
# Store the fraction (num/total) in your list as
`frac_male_increase`.
# How many counties experienced an increase in female binge
drinking between
# 2002 and 2012?
# Store the number in your list as `num_female_increase`.
# What fraction of counties experienced an increase in female binge
drinking
# between 2002 and 2012?
# Store the fraction (num/total) in your list as
`frac_female_increase`.
# How many counties experienced a rise in female binge drinking
*and*
# a decline in male binge drinking?
# Store the number in your list as `num_f_increase_m_decrease`.
# Convert your list to a data frame, and write the results
# to the file `binge_info.csv`
# The next questions return *data frames as results*:
# What is the *minimum* level of binge drinking in each state in
2012 for
# both sexes (across the counties)? Your answer should contain
roughly 50 values
# (one for each state), unless there are two counties in a state
with the
# same value. Your answer should be a *dataframe* with the
location, state, and
# 2012 binge drinking rate. Write this to a file called
`min_binge.csv`.
# What is the *maximum* level of binge drinking in each state in
2012 for
# both sexes (across the counties)? Your answer should contain
roughly 50 values
# (one for each state), unless there are two counties in a state
with the
# same value. Your answer should be a *dataframe* with the
location, state, and
# 2012 binge drinking rate. Write this to a file called
`max_binge.csv`.
################################# Joining Data
#################################
# You'll often have to join different datasets together in order to
ask more
# involved questions of your dataset. In order to join our datasets
together,
# you'll have to rename their columns to differentiate them.
# First, rename all prevalence columns in the any_drinking dataset
to the
# have prefix "any_" (i.e., `males_2002` should now be
`any_males_2002`)
# Hint: you can get (and set!) column names using the colnames
function.
# This may take multiple lines of code.
# Then, rename all prevalence columns in the binge_drinking dataset
to the have
# the prefix "binge_" (i.e., `males_2002` should now be
`binge_males_2002`)
# This may take multiple lines of code.
# Then, create a dataframe by joining together the both
datasets.
# Think carefully about the *type* of join you want to do, and what
the
# *identifying columns* are. You will use this (joined) data to
answer the
# questions below.
# Create a column `diff_2012` storing the difference between `any`
and `binge`
# drinking for both sexes in 2012
# Which location has the greatest *absolute* difference between
`any` and
# `binge` drinking? Your answer should be a one row data frame with
the state,
# location, and column of interest (diff_2012).
# Write this dataframe to `biggest_abs_diff_2012.csv`.
# Which location has the smallest *absolute* difference between
`any` and
# `binge` drinking? Your answer should be a one row data frame with
the state,
# location, and column of interest (diff_2012).
# Write this dataframe to `smallest_abs_diff_2012.csv`.
############## Write a function to ask your own question(s)
####################
# Even in an entry level data analyst role, people are expected to
come up with
# their own questions of interest (not just answer the questions
that other
# people have). For this section, you should *write a function*
that allows you
# to ask the same question on different subsets of data. For
example, you may
# want to ask about the highest/lowest drinking level given a state
or year.
# The purpose of your function should be evident given the input
parameters and
# function name. After writing your function, *demonstrate* that
the function
# works by passing in different parameters to your function.
################################### Challenge
##################################
# Using your function from part 1 that wrote a .csv file given a
state name,
# write a separate file for each of the 51 states (including
Washington D.C.)
# The challenge is to do this in a *single line of (very concise)
code*
# Write a function that allows you to pass in a *dataframe* (i.e.,
in the format
# of binge_drinking or any_drinking) *year*, and *state* of
interest. The
# function should saves a .csv file with observations from that
state's counties
# (and the state itself). It should only write the columns `state`,
`location`,
# and data from the specified year. Before writing the .csv file,
you should
# *sort* the data.frame in descending order by the both_sexes
drinking rate in
# the specified year. The file name should have the format:
# `DRINKING_STATE_YEAR.csv` (i.e. `any_Utah_2005.csv`).
# To write this function, you will either have to use a combination
of dplyr
# and base R, or confront how dplyr uses *non-standard
evaluation*
# Hint:
https://github.com/tidyverse/dplyr/blob/34423af89703b0772d59edcd0f3485295b629ab0/vignettes/nse.Rmd
# Hint:
https://www.r-bloggers.com/non-standard-evaluation-and-standard-evaluation-in-dplyr/
# Create the file `binge_Colorado_2007.csv` using your
function.
In: Computer Science
Create a UI that has a field in which to enter text, a field to display read-only text and a button. When the button is pressed, take the value of the text box and try to create a month object. If a number was entered, use the constructor that accepts an integer. If alphabetic characters were entered, use the constructor that accepts a string. If nothing is entered, use the no-argument constructor. If a valid month object is created, use the toString function on the month object to display info about the month. Otherwise, display an appropriate error message.
Create a UI with two drop-down lists, a button, and a field to display read-only text. One drop-down has the values 1-12. The other has the names of the months. When the button is pressed, create two-month objects using the appropriate constructors, then display if the months are equal, if month 1 > month 2 or if month 2 > month 1.
TWO SEPERATE USER INTERFACES.
THIS IS TO BE DONE USING JAVA
In: Computer Science