Answer:
Stack
Explanation:
Stack is a linear data structure that follows a particular order in the way an operation is done or sequence a job is completed.
It uses either LIFO ( Last In, First Out) which is also known as first come first served sequence or FILO (First In, Last Out) sequence.
There are several real life examples of which we can use the example of replacing the snack items Sarah brought for the customer.
If Sarah used the LIFO method, it means she replaced the snack items first ontop of the already existing snack items that's why there is a mismatch.
Answer:
Here is the Python program:
d = {5:3, 4:1, 12:2}
val_of_max = d[max(d.keys())]
print(val_of_max)
Explanation:
The program works as follows:
So we have a dictionary named d which is not empty and has the following key-value pairs:
5:3
4:1
12:2
where 5 , 4 and 12 are the keys and 3, 1 and 2 are the values
As we can see that the largest key is 12. So in order to find the largest key we use max() method which returns the largest key in the dictionary and we also use keys() which returns a view object i.e. the key of dictionary. So
max(d.keys()) as a whole gives 12
Next d[max(d.keys())] returns the corresponding value of this largest key. The corresponding value is 2 so this entire statement gives 2.
val_of_max = d[max(d.keys())] Thus this complete statement gives 2 and assigns to the val_of_max variable.
Next print(val_of_max) displays 2 on the output screen.
The screenshot of program along with its output is attached.
Answer:
// program in C++ to check leap year.
// include header
#include<iostream>
using namespace std;
// main function
int main() {
// variable
int inp_year ;
// ask user to enter year
cout<<"Enter year:";
// read year
cin>>inp_year;
// check year is leap or not
if (((inp_year % 4 == 0) && (inp_year % 100 != 0)) || (inp_year % 400 == 0))
// if leap year , print leap year
cout<<inp_year<<" is a leap year.";
else
// print not leap year
cout<<inp_year<<" is not a leap year.";
return 0;
}
Explanation:
Read year from user.Then check if year is divisible by 4 and not divisible by 100 or year is divisible by 400 then year is leap year.otherwise year is not leap year.
Output:
Enter year:1712
1712 is a leap year.
Answer:
int switch_1,switch_2;
int get_first_switch_state()
{
return switch_1;
}
int get_second_switch_state()
{
return switch_1;
}
int get_lamp_state()
{
if((get_first_switch_state())
if(get_second_switch_state()) return 1;
else
if(!get_second_switch_state()) return 1;
return 0;
}
void toggle_first_switch()
{
if(get_first_switch_state()) switch_1=0;
else switch_1=1;
}
void toggle_second_switch()
{
if(get_second_switch_state()) switch_2=0;
else switch_2=1;
}
Explanation:
Answer:
Written using Python 3:
<em>num1 = int(input("Enter first value: "))
</em>
<em>num2 = int(input("Enter second value: "))
</em>
<em>
</em>
<em>finalResult =(num1+num2) / 3
</em>
<em>print("Answer is: ", finalResult)</em>
<em />
INPUT:
Enter first value: 4
Enter second value: 5
OUTPUT:
Answer is: 3
Explanation:
First step is to declare the variables:
Second is to ask for an input:
- input("Enter first value: ")
Then we need to convert them into integers by encapsulating the line above inside int(). This is a requirement before you can do any mathematical operations with the given values.
- num1 = int(input("Enter first value: "))
Next is to calculate:
- <em>finalResult </em><em>=(num1+num2) / 3
</em>
- Here we first add the values of num1 and num2, <em>then </em>divide the sum by 3.
And to test if the output is correct, let us print it:
- <em>print("Answer is: "</em><em>,</em><em> </em><em>finalResult</em><em>)</em>
- print() - when used for strings/texts, use quotation marks like: "text here"
- but when used to print variables and numbers (integers, floats, etc), there is no need to enclose them with quotation marks.
- to concatenate string and an integer (in this case the variable finalResult), simply add a comma right after the string.
<em />