Introduction to Computers -Final exam- 授課教授:李錫智. Q1 Write a function named Reverse...

22
Introduction to Computers -Final exam- 授授授授 授授授

Transcript of Introduction to Computers -Final exam- 授課教授:李錫智. Q1 Write a function named Reverse...

Page 1: Introduction to Computers -Final exam- 授課教授:李錫智. Q1 Write a function named Reverse that takes a string as input and returns a copy of that string in reverse.

Introduction to Computers

-Final exam-授課教授:李錫智

Page 2: Introduction to Computers -Final exam- 授課教授:李錫智. Q1 Write a function named Reverse that takes a string as input and returns a copy of that string in reverse.

Q1

• Write a function named Reverse that takes a string as input and returns a copy of that string in reverse order. For example, the function call Reverse("apple") should return the string "elppa".

Page 3: Introduction to Computers -Final exam- 授課教授:李錫智. Q1 Write a function named Reverse that takes a string as input and returns a copy of that string in reverse.

Solution of Q1

• Ans:

function Reverse(str)

{

var len,restr;

len=str.length;

restr="";

i=len-1;

while(i!=-1)

{

restr=restr+str.charAt(i);

i=i-1;

}

}

Page 4: Introduction to Computers -Final exam- 授課教授:李錫智. Q1 Write a function named Reverse that takes a string as input and returns a copy of that string in reverse.

Q2

• Consider the following program :function string(string){

str = string;i = str.search(/[aeiou]/);while(i != -1){

str = str.substring(0,i) + '*' + str.substring(i+1,str.length);

i = str.search(/[aeiou]/);}return str;

}

• What will be returned for the function call string('Hello world')?

Page 5: Introduction to Computers -Final exam- 授課教授:李錫智. Q1 Write a function named Reverse that takes a string as input and returns a copy of that string in reverse.

Solution of Q2

• Ans: H*ll* w*rld

Page 6: Introduction to Computers -Final exam- 授課教授:李錫智. Q1 Write a function named Reverse that takes a string as input and returns a copy of that string in reverse.

Q3

• Write a function named Maximum that takes an array of numbers as input and return the biggest value in the array. For example, the call Maximum([2, 28, 33, 12, 5, 17, 8]) should return 33.

Page 7: Introduction to Computers -Final exam- 授課教授:李錫智. Q1 Write a function named Reverse that takes a string as input and returns a copy of that string in reverse.

Solution of Q3

ZenLong He
function Maximum(array)// Find maximum value from array.{var index, Max;index = 0;//Initialize Indexwhile (index < array.length){if (index == 0) {//Set the value of maximum at first time.Max = array[index];} else {if (array[index] > Max) {Max = array[index];}}index = index + 1;}return Max;}
Page 8: Introduction to Computers -Final exam- 授課教授:李錫智. Q1 Write a function named Reverse that takes a string as input and returns a copy of that string in reverse.

Q4

• Suppose you are given the following segment of program:

Phrase = “George Bush, USA\n”+“Leonardo da Vinci, Italy\n”+“William Shakespeare, England\n”;Spilt_1 = Phrase.split(“, “);Spilt_2 = Phrase.split(/[,\n]+/);

• [5] What is the value of Split_1?

• [5] What is the value of Split_2?

Page 9: Introduction to Computers -Final exam- 授課教授:李錫智. Q1 Write a function named Reverse that takes a string as input and returns a copy of that string in reverse.

Solution of Q4

• Ans:• Split_1 = ["George Bush", "USA\nLeonardo da Vinci",

"Italy\nWilliam Shakespeare", "England\n"] ;• Split_2 = ["George Bush", "USA", "Leonardo da Vinci",

"Italy", "William Shakespeare", "England"];

ZenLong He
<HTML><HEAD><SCRIPT TYPE="text/javascript">function func(){var show_1, show_2;show_1 = "";show_2 = "";Phrase = "George Bush, USA\n"+"Leonardo da Vinci, Italy\n"+"William Shakespeare, England\n";Spilt_1 = Phrase.split(", ");Spilt_2 = Phrase.split(/[,\n]+/);i = 0;while(i < Spilt_1.length){show_1 = show_1 + Spilt_1[i] + ",";i = i + 1;}document.getElementById('showbox_1').value = show_1;i = 0;while(i < Spilt_2.length){show_2 = show_2 + Spilt_2[i] + ",";i = i + 1;}document.getElementById('showbox_2').value = show_2;}</SCRIPT></HEAD><BODY><INPUT type="button" value="Click here" onclick="func();" /><br />Answer:<br /><INPUT type="text" id="showbox_1" size="40" value="" /><INPUT type="text" id="showbox_2" size="40" value="" /></BODY></HTML>
Page 10: Introduction to Computers -Final exam- 授課教授:李錫智. Q1 Write a function named Reverse that takes a string as input and returns a copy of that string in reverse.

Q5

• Please using the NOT, AND, and OR gates, draw a circuit that will output 0 while both inputs are 0 or 1. That is, suppose A and B are the two inputs and C is the output, then C=0 if and only if (A=0 and B=0) or (A=1 and B=1).

Page 11: Introduction to Computers -Final exam- 授課教授:李錫智. Q1 Write a function named Reverse that takes a string as input and returns a copy of that string in reverse.

Solution of Q4

A B C

0 0 0

0 1 1

1 0 1

1 1 0

Page 12: Introduction to Computers -Final exam- 授課教授:李錫智. Q1 Write a function named Reverse that takes a string as input and returns a copy of that string in reverse.

Q6• Consider the following program:

• What will be returned for the function call Cipher('Bronze')?

function Cipher(str){

var coded, ch, index, key, i;alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';coded = ' ';key = 4;str = str.toUpperCase();i = 0;while(i < str.length){

ch = str.charAt(i);index = alphabet.search(ch);if (index != -1) {

if ( (index+key) < alphabet.length) {coded = coded + alphabet.charAt(index+key);

}else {coded = coded + alphabet.charAt(index+key-alphabet.length);

}}else {

coded = coded + ch;}i = i + 1;

}return coded;

}

Page 13: Introduction to Computers -Final exam- 授課教授:李錫智. Q1 Write a function named Reverse that takes a string as input and returns a copy of that string in reverse.

Solution of Q6

• Ans: FVSRDI

Offset 4 characters.

ZenLong He
<HTML><HEAD><script type="text/javascript">function cipher(str){var coded, ch, index, key, i;alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';coded = '';key = 4;str = str.toUpperCase();i = 0;while(i < str.length){ch = str.charAt(i);index = alphabet.search(ch);if (index != -1) {if ( (index+key) < alphabet.length) {coded = coded + alphabet.charAt(index+key);}else{coded = coded + alphabet.charAt(index+key-alphabet.length);}}else{coded = coded + ch;}i = i + 1;}return coded;}</script></HEAD><BODY>Enter your message below:<br /><INPUT type="text" id="Plaintext" size="20" value="" /><br /><INPUT type="button" value="Ceaser's Cipher" onclick="str = document.getElementById('Plaintext').value;document.getElementById('Ciphertext').value = cipher(str);" /><INPUT type="text" id="Ciphertext" size="20" value="" /><br /></BODY></HTML>
Page 14: Introduction to Computers -Final exam- 授課教授:李錫智. Q1 Write a function named Reverse that takes a string as input and returns a copy of that string in reverse.

Q7

• Suppose you are given an array of numbers, A.

• (a) [10] Please write a function taking A and returning an array B which records the number of times each element appearing in A. For example, suppose A=[15,25,35,45,35,35,25]. Then B=[1,2,3,1,3,3,2].

• (b) [10] Please write a function taking A and returning the element in A which appears most frequently in A. For example, for the above example, 35 will be returned.

Page 15: Introduction to Computers -Final exam- 授課教授:李錫智. Q1 Write a function named Reverse that takes a string as input and returns a copy of that string in reverse.

Solution of Q7

ZenLong He
function func_A(A){var B, i, j;B = [];i = 0;while(i < A.length){j = 0;B[i] = 0;while(j < A.length){if(A[i] == A[j]){B[i] = B[i] + 1;}j = j + 1;}i = i + 1;}return B;}
ZenLong He
function func_B(A){var B, i, j, f_most, f_most_index;B = [];f_most = 0;i = 0;while(i < A.length){j = 0;B[i] = 0;while(j < A.length){if(A[i] == A[j]){B[i] = B[i] + 1;}j = j + 1;}if (B[i] > f_most) {f_most = B[i];f_most_index = i;}i = i + 1;}return A[f_most_index];}
Page 16: Introduction to Computers -Final exam- 授課教授:李錫智. Q1 Write a function named Reverse that takes a string as input and returns a copy of that string in reverse.

Q8

• Please design a Web page that asks the user to enter his/her name and age in a phrase separated by a colon, e.g., John :36.

• When the user clicks a button, the webpage will show the name and say the user is an adult or a child, e.g., John is an adult.

• Note that a person is an adult if his/her age is equal to or greater than 20.

Page 17: Introduction to Computers -Final exam- 授課教授:李錫智. Q1 Write a function named Reverse that takes a string as input and returns a copy of that string in reverse.

Solution of Q8

ZenLong He
<HTML><HEAD><SCRIPT TYPE="text/javascript">function func(str){var name, age, word, answer;answer = "";word = str.split(":");name = word[0];age = word[1];age = parseFloat(age);if(age>=20){answer = name + " is an adult." ;}else if(age < 20){answer = name + " is a child.";}else{answer = "Format error!";}return answer;}</SCRIPT></HEAD><BODY>Please enter your information:<br /><INPUT type="text" id="box" size="20" value="" /><br /><br /><INPUT type="button" value="Click here" onclick="str = document.getElementById('box').value;document.getElementById('showbox').value = func(str);" /><br />Answer:<br /><INPUT type="text" id="showbox" size="40" value="" /></BODY></HTML>
Page 18: Introduction to Computers -Final exam- 授課教授:李錫智. Q1 Write a function named Reverse that takes a string as input and returns a copy of that string in reverse.

Q9

• Consider the following circuit. A, B, and C are inputs. P and Q are outputs. Please list the input-output relationship in a truth table.

Page 19: Introduction to Computers -Final exam- 授課教授:李錫智. Q1 Write a function named Reverse that takes a string as input and returns a copy of that string in reverse.

Solution of Q9

A B C Q P

0 0 0 0 0

0 0 1 0 1

0 1 0 0 1

0 1 1 1 0

1 0 0 0 1

1 0 1 1 0

1 1 0 1 0

1 1 1 1 1

Page 20: Introduction to Computers -Final exam- 授課教授:李錫智. Q1 Write a function named Reverse that takes a string as input and returns a copy of that string in reverse.

Q10

• Please write a function to check whether a given string is a mirror-string. A string A is a mirror-string if and only if A satisfies one of the following conditions:• A is a concatenation of B and C, i.e., A=BC, where B

and C are strings and C is the reverse of B. For example, “abccba” is a mirror-string.• A is a concatenation of B, t, and C, i.e., A=BtC, where B

and C are strings, t is a character, and C is the reverse of B. For example, “abcycba” is a mirror-string.

Page 21: Introduction to Computers -Final exam- 授課教授:李錫智. Q1 Write a function named Reverse that takes a string as input and returns a copy of that string in reverse.

Solution of Q10

• Ans :

function IsP(string) {

var stripped, front, back;

stripped = string;

front = 0;

back = stripped.length - 1;

while (front < back) {

if (stripped.charAt(front) != stripped.charAt(back)) {

return false;

}

front = front + 1;

back = back - 1;

}

return true;

}

Page 22: Introduction to Computers -Final exam- 授課教授:李錫智. Q1 Write a function named Reverse that takes a string as input and returns a copy of that string in reverse.

The end of the final Exam