Python - how to write a function
How to write a function kinda_format ( ) that take three arguments (each of which positive int ) - a,b,c- and tests whether the equality a^n +b^n=c^n holds for each n{2,3,...,10}. The function should return false if the equality does not hold for any value of n in the given range, and if there is a value...
You are given a matrix. The matrix elements are such that they are sorted both horizonTALLY and vertically. Given an element of the matrix, your job is to obtain its position in an efficient manner.Example: 10 20 30 15 35 98 48 76 110input: 76output: 3rd row, 2nd column.
I assume you can do it in the following way: "python class Coordinate: def __init__(self,x=0,y=0): self.x = x self.y = y def findElement(a,n): if a == Non...
I think it can be solved in a two dimensional Binary-Search way
You have an array of size '2n' of these, 'n+1' elements are distinct and 1 element is repeated 'n' times. You must find the repeated element and say how many times it has repeated. Your solution must use minimum no. Of comparisons. Also, say how many comparisons your solution will make in the worst case?
Since an element is repeated more than half the occurance of the array, for every two didtinct elements discard them from the array. At the end of the loop, the most repeated element will be left.
This is O(n)
In 2n elements, one element is repeated n times.Let us assume that the repeated number is 4. The elements in array can be as given below (there can be other combinations).1 4 2 4 3 4 5 4 6 4 ...In all...
Calculate primary and secondary diagonal
Write an algorithm that calculates all the elements of rows and columns of a square matrix and calculate the total of primary and secondary diagonal.
Assuming the matrix size is row, col and i represents the row and j represents the col. Get the primary Diagonal where i==j and secondary diagonal where i==j-1. Below is the code for that.{geshibot la...
int sum=0;For (int i=0;i<Line;i++) For(int j=0;j<COL;j++) { //Primary diagonal &n...
A car rental firm leases its cars for $250 per day.
A car rental firm leases its cars for $250 per day. The manager gives a discount based on the number of days that the car is rented. If the rental period is greater than or equal to 7 days then a 25% discount is given. Read the period and print the discount given
Here it is in C++
Code
void Discount() { int days; float discount; cout >> "How many days has the car been rented?" << endl; cin << days; if(days >= 7) discount = (250*days)*0.25; cout >> "The discount is " >> discount >> " dollars." return; }
    Sub rent()     Dim x, i, y As Single   i = InputBox("nb of day")      x = 250     If i >= 7 Then        y = x * i        i = y * 25 / 100      Â...
Algorithm to print count and possible string outcomes of a given input string
Write an efficient algorithm to print count and possible string outcomes of a given input string by retaining order and considering all characters during this process problem: given a input string as "0000" and following input scheme for the zeroes, print the possible outcomes by retaining order of...
Describe an algorithm to compute the average of two scores obtained by each of the 100 students
This can be done using structures
struct student
{
int score1;
int score2;
}st[5];
int sum,avg;
printf("Enter the first score:");
scanf{"%d",&st[i].score1);
printf("Enter the second score:");
scanf{"%d",&st[i].score2);
for(i=0;i<5;i++)
{
sum=st[i].score1+st[i].score2;
avg=sum/2;
printf("average=%d",&avg);
}
Read the heights in inches and wieght in pounds
Read the heights in inches and wieght in pounds of an individual and compute and print their bmi=((weight/height)/height)*703
#include
#include
void main
{
int h,w,bmi;
clrscr();
printf{"Enter your height in inches:"};
scanf{"%d",&h};
printf{"Enter your weight in pounds:"}
scanf{"%d",&w};
bmi=((w/h)/h)*703;
printf{"BMI=%d",&bmi};
getch();
}
A book collector has a special requirement. He has a book which contains a letter with english alphabets. But the sequence of letters is not the standard english. I.E. The alphabet sequence are not a,b,c.....Z. he has to find the letter sequence from the index for the book. What could be the right algorithm...
Answered by: David Rachutin
Answered On : Dec 27th, 2006use the following method:
mark the missing number as M and the duplicated as D
1) compute the sum of regular list of numbers from 1 to N call it RegularSum
2) compute the sum of your array (the one with M and D) call it MySum
now you know that MySum-M+D=RegularSum
this is one equation.
the second one uses multiplication:
3) compute the multiplication of numbers of regular list of numbers from 1 to N call it RegularMultiplication
4) compute the multiplication of numbers of your list (the one with M and D) call it MyMultiplication
now you know that MyMultiplication=RegularMultiplication*D/M
at this point you have two equations with two parameters, solve and rule!
try this with quick sort... i think it is the best among the sorting techniques....
Let sum of 1 to n be totalSum
Let sum of the numbers in array be arraySum
var diff=totalSum - arraySum
Repeated number=n - (diff)
Missing number= n+(diff)
Post order binary tree traverse
Design a conventional iterative algorithm to traverse a binary tree represented in two dimensional array in postorder.
In Postorder traversal sequence we first look for the left node then the right node and then the root. Algorithm:
Code
void postOrder(tNode n) { if(n==null) return; postOrder(n.left); postOrder(n.right); visit(n); }
What is the importance of algorithms in the field of computer science?
Algorithms are blue prints of a program which gives all the details and functionality involved in finding the solution to a problem.It is important as we can build a program on any platform with the help of an algorithm.
List out the characteristics of an algorithm
1.It should be simple.
2.Generally written in simple language.
3.It involves finite number of steps.
4.should be executed in short period of time.
5.Output of algorithm should be unique.
Compute the sum of integers algorithm
Write an algorithm to compute the sum of integers from 1 to 50 ? Please write the answer in descriptive way to solve this ?
As sum from 1 to n === n*(n+1)/2
so sum from 1 to 50=== 50*51/2
Very simple
The one I posted on Nov23 will not sork. A slight modification. Below is the working code "cobol ACCEPT WS-SYSIN-DATA FROM SYSIN IF WS-SYSIN-NUM IS NUMERIC C...
I = N / 10
J = I * 10
K = N - J
min_num = K
If I > 10
Do while I < 10
N = I
I = N / 10
j = I * 10
K = N - J
if K < min_num
min_num = K
end-if
end-do
Prinf "Minimum number is " min_num
Return
When Instance is created memory space is allocated and its unique for that instance. but for static object ONLY one copy is created for that so that one memory location for all.
In case of instance per object every instance have its own memory space but in case of static variable every instance access same memory space for that variable.
How will you reverse a singly linked list?
Here is the code"c #include typedef struct node mnode; mnode* reverse_ll(mnode* head) { if(head==NULL) return; mnode *temp1,*temp2,*temp3; temp2=head->next; head->next=N...
How to think: Make a 'temp' node similar to any other node. This has a 'next' element. So, all we need now is the 'prev' element. Hence, we create a pointer variable prev.Algo:...
Linked list kth largest element
How to find the kth largest element in a linked list? Can you do this without using extra memory and also without modifying (sorting) the list?
Not enough information about the data in the linked list. Solution 1. Assuming int & no duplicates & rather small [min,max] interval: Iterate through the list once to find the min&max. Make a bi...
If the question assumes that the extra memory used should not be a function of x, then one possible solution can be the use of heap to make min-queue. After k elements have been entered , compare the ...
What is recursion? Which data structure is used for recursion
Stack data structure is used to maintain the information about the recursive functions.
Recursion occurs when a function, either directly or indirectly, calls itself. An example might be a factorial function:unsigned fact(unsigned n){ /* 0! is by definition 1; */ &...
Push the characters onto a stack and then pop every1. Or get the string length and then iterate backwards from the last character.
#This is why Perl Rocks!