You are on page 1of 6

QUERIES

1. I used python indexing rather than pseudo code indexing because of the Hint in
question missing_char.
2. Question string_bits requires error handling since an indexing error may come up
if there are even number of characters in the string. I don't know how to hadnle
errors in pseudo code.
3. May be less than 4 means cannot be more than four? I made program considering
that there will be 4 or less

sleep_in
FUNCTION sleep_in(weekday : BOOLEAN, vacation : BOOLEAN) RETURNS BOOLEAN
DECLARE is : BOOLEAN
is <-- TRUE
IF weekday = NOT(TRUE) OR vacation = TRUE
THEN
is <-- TRUE
ELSE
is <-- FALSE
END IF
RETURN is
ENDFUNCTION

monkey_trouble
FUNCTION monkey_trouble(a_smile : BOOLEAN, b_smile : BOOLEAN) RETURNS
DECLARE trouble : BOOLEAN
IF a_smile = TRUE AND b_smile = TRUE OR a_smile = FALSE AND b_smile = FALSE
THEN
trouble <-- TRUE
ELSE
trouble <-- FALSE
END IF
RETURN trouble
ENDFUNCTION

sum_double
FUNCTION sum_double(One : INTEGER, Two : INTEGER) RETURNS INTEGER
DECLARE sum : INTEGER
IF One = Two
THEN
sum <-- 2 * (One + Two)
ELSE
sum <-- One + Two
END IF
RETURN sum
ENDFUNCTION

diff21
FUNCTION diff21(n : INTEGER) RETURNS INTEGER
DECLARE difference
IF n > 21
THEN
difference <-- 2 * (n-21)
ELSE
difference <-- 21 - n
RETURN difference
ENDFUNCTION

parrot_trouble
FUNCTION parrot_trouble(talking : BOOLEAN, hour : INTEGER) RETURNS BOOLEAN
DECLARE trouble : BOOLEAN
IF talking = TRUE AND hour < 7 OR talking = TRUE AND hour > 20:
THEN
IF hour > 23 OR hour < 0
THEN
OUTPUT "Error in mentioning Time"
ELSE
trouble <-- TRUE
ENDIF
ELSE
trouble <-- FALSE
ENDIF
RETURN trouble
ENDFUNCTION

makes_10
FUNCTION makes10(One : INTEGER, Two : INTEGER) RETURNS INTEGER
DECLARE answer : BOOLEAN
DECLARE sum : INTEGER
sum <-- One + Two
IF sum = 10 OR a = 10 OR b = 10
THEN
answer <-- TRUE
ELSE
answer <-- FALSE
ENDIF
RETURN answer
ENDFUNCTION

near_hundred
FUNCTION near_hundred(n : INTEGER) RETURNS BOOLEAN
DECLARE check : BOOLEAN
IF abs(n-100) <= 10 OR abs(n-200) <= 10
THEN
check <-- TRUE
ELSE
check <-- FALSE
ENDIF
RETURN check
ENDFUNCTION

pos_neg
FUNCTION pos_neg(One : INTEGER, Two : INTEGER, negative : BOOLEAN) RETURNS BOOLEAN
DECLARE answer : BOOLEAN
DECLARE anchor, Oneans, Twoans : INTEGER
anchor <-- 0
Oneans <-- 0 + One
Twoans <-- 0 + Two
IF negative = TRUE
THEN
IF Oneans < 0 AND Twoans > 0 OR Oneans > 0 AND Twoans < 0
THEN
answer <-- TRUE
ELSE
answer <-- FALSE
ENDIF
ENDIF
RETURN answer
ENDFUNCTION

not_string
FUNCTION not_string(string : STRING) RETURNS STRING
DECLARE new_string
IF string[0] = "n" AND string[1] = "o" AND string[2] = "t" AND string[3] = "
"
THEN
new_string <-- string
ELSE
new_string <-- "not " & string
ENDIF
RETURN new_string
ENDFUNCTION

missing_char
FUNCTION missing_char(string : STRING, n: INTEGER) RETURNS STRING
DECLARE new_string, substrL, substrR : STRING
IF n = 0
THEN
new_str <-- SUBSTR(string, 1, LENGTH(string)-1)
ELSE
IF n = LENGTH(string)-1
THEN
new_str <-- SUBSTR(string, 0, n-1)
ELSE
substrL <-- SUBSTR(string, 0 , n-1)
substrR <-- SUBSTR(string, n+1, LENGTH(string)-1)
new_string <-- substrL & substrR
ENDIF
ENDIF

RETURN new_string
ENDFUNCTION

front_back
FUNCTION front_back(string : STRING) RETURNS STRING
DECLARE newstr : STRING
emptystr <-- ""
FOR index <-- 0 TO LENGTH(string)-1
newstr <-- string[index] & emptystr
NEXT FOR
RETURN newstr
ENDFUNCTION

front3
FUNCTION front3(string : STRING) RETURNS STRING
DECLARE front : STRING
front <-- ""
FOR index <-- 0 TO 2
front <-- front & string
NEXT FOR
front <-- front & front & front
RETURN front
ENDFUNCTION

front_times
FUNCTION front_times(string : STRING, n : INTEGER) RETURNS STRING
DECLARE front : STRING
front <-- ""
IF LENGTH(string) > 3
THEN
FOR index <-- 0 TO 2
front <-- front & string
NEXT FOR
ELSE
front <-- string
ENDIF
FOR counter <-- 1 TO (n-1)
front <-- front & front
NEXT counter
RETURN front
ENDFUNCTION

string_bits
FUNCTION string_bits(string : STRING) RETURNS STRING
DECLARE new_string: string
FOR index <-- 0 TO maxIndex
IF index = 0
THEN
new_string <-- string[index]
ELSE
new_string <-- string[index+1]
ENDIF
NEXT index
RETURN new_string
ENDFUNCTION

string_splosion
FUNCTION string_splosion(string : STRING) RETURNS STRING
DECLARE new_string : STRING
new_string <-- ""
#1st print [0] * 2
#2nd print [1] * 1
#3rd print [0,1,2] * 1
#4th print [0,1,2,3,4]
FOR index <-- 0 TO LENGTH(string)-1
IF index = 0
THEN
new_string <-- new_string & string[0]
new_string <-- new_string & string[0]
new_string <-- new_string & string[0]
new_string <-- new_string & string[0]
ENDIF
IF index = 1
THEN
new_string <-- new_string & string[1]
ENDIF
IF index = 2
THEN
new_strin <-- new_string & string[0] & string[1] and
string[2]
ENDIF
IF index = 3
THEN
new_string <-- new_string & string
ENDIF
NEXT index
RETURN new_string
ENDFUNCTION

array_count9
FUNCTION array_count9(array : ARRAY OF INTEGER) RETURNS INTEGER
DECLARE no9s : INTEGER
no9s <-- 0
FOR index <-- 0 TO LENGTH(array)-1
IF array[index] = 9:
THEN
no9s <-- no9s + 1
ENDIF
NEXT index
RETURN no9s
ENDFUNCTION

array_front9
FUNCTION array_count9(array : ARRAY OF INTEGER) RETURNS BOOLEAN
DECLARE 9is : BOOLEAN
9is <-- FALSE
FOR number <-- 0 TO LENGTH(array)-1
IF array[number] = 9
THEN
9is <-- TRUE
NEXT number
RETURNS 9is
ENDFUNCTION

array123
FUNCTION array123(array : ARRAY OF INTEGER) RETURNS BOOLEAN
DECLARE appears : BOOLEAN
appears <-- FALSE
FOR index <-- 0 TO LENGTH(array)-1
IF array[index] = 1 AND array[index+1] = 2 AND array[index+2] = 3
THEN
appears <-- TRUE
ENDIF
NEXT index
RETURN appears
ENDFUNCTION

string_match
FUNCTION string_match(a : STRING, b : STRING) RETURNS INTEGER
DECLARE position : INTEGER
DECLARE appears : BOOLEAN
DECLARE shortLength
position <-- 0
appears <-- FALSE
IF LENGTH(a) > LENGTH(b):
THEN
shorterLength = LENGTH(b)
ELSE
shorterLength = LENGTH(a)
ENDIF
FOR index <-- 0 TO shortLength - 1
IF a[index] = a[index+1]
THEN
IF b[index] = b[index+1]
THEN
position <-- index + 1

ELSE
position <-- 0
ENDIF
ENDIF
NEXT index
RETURN position
ENDFUNCTION

hello_name
FUNCTION hello_name(string : STRING) RETURNS STRING
DECLARE new_string
new_string <-- "Hello " & string & "!"
RETURN new_string
ENDFUNCTTION

make_abba
FUNCTION make_abba(a : STRING, b : STRING) RETURNS STRING
DECLARE new_string
new_string <-- b & a
new_string <-- a & b & new_string
RETURN new_string
ENDFUNCTION

You might also like