Author: kv.sridhar sai
Writing comments using DSL:
single line comments:
anything after '#' will be considered as a comment in DSL.
DSL CODE:
#StartCode
Python Code Generated by above Written DSL:
#Write Your Code Here
Data types Supported by DSL:
- boolean
- character
- integer
- long_integer
- float
- double
- string
Reading and printing variables in DSL:
To read a variable the DSL Statement is :
Datatype(variable_name).
Example:
to read an integer into a variable n. The DSL Statement will be
DSL Code:
#(While reading an integer)
integer(n)
Python code Generated by the above Written DSL:
n = int(raw_input().strip())
DSL Code:
#(While reading a float)
float(m)
Python Code Generated by above Written DSL:
m = int(raw_input().strip())
This syntax is the same for every Data type that DSL Supports.
printing a variable:
DSL syntax :
print(VARIABLE_DATATYPE, VARIABLE_NAME)
print(integer,cse)
Read and Print a 1D Array:
Two ways of reading the input into arrays:
- We can read the array given that the elements are space separated.
DSL:
array(DATA_TYPE,VARIABLE_NAME,ELEMENT_COUNT,single)
- We can read the array such that elements are given each on a new line.
DSL:
array(DATA_TYPE,VARIABLE_NAME,ELEMENT_COUNT,multi)
Printing an array:
print(DATATYPE_array,VARIABLE_NAME)
Example:
DSL:
#reading the input into a float array
array(float,cse_array, 6, single)
#printing a list
print(float_array,cse_array)
Python Code Generated by above Written DSL:
arr = map(float, raw_input().rstrip().split())
#these lines show that output is being written to stdout file.
sys.stdout.write(' '.join(map(str, arr)))
sys.stdout.write('\n')
Declaring a function and Invoking it:
The DSL syntax to Declare a function is:
function(RETURN_TYPE, FUNCTION_NAME, PARAM1—TYPE PARAM1—NAME,....)
DSL Code:
function(integer, productOfTwoIntegers, integer a, integer b)
The DSL syntax to invoke a function is:
invoke(RETURN_TYPE, RETURN_VARIABLE_NAME, FUNCTION_NAME, Param1-Name,Param-2-Name,...)
DSL Code:
invoke(integer,cse_result,productOfTwoIntegers,a,b)
Both the functions together will Generate this:
DSL Code:
function(integer, productOfTwoIntegers, integer a, integer b)
integer(a)
integer(b)
invoke(integer, cse_result, productOfTwoIntegers, a, b)
print(integer,cse_result)
Python Code Generated by above Written DSL:
def sumOfTwoIntegers(a, b):
# Write your code here
if __name__ == '__main__':
a = int(raw_input().strip())
b = int(raw_input().strip())
cse_result = productOfTwoIntegers(a, b)
sys.stdout.write(str(cse_result) + '\n')
Loops in DSL:
DSL also supports loops. The DSL syntax for loops:
loop(VARAIBLE_NAME)
VALID_DSL_STATEMENTS
endloop
Example:
DSL Code:
integer(n)
loop(n)
integer(x)
print(integer,x)
endloop
Python code Generated by above Written DSL:
if __name__ == '__main__':
n = int(raw_input().strip())
for n_itr in xrange(n):
a = float(raw_input().strip())
sys.stdout.write(str(a) + '\n')
Problem statement that is given to First year students as part of Ascii's CSE - A Survival Guide Workshop
The ASCII Problem:
Anirudh is a first year student who just learnt that computers convert characters into ASCII values to process them.
As an assignment, his teacher gives him the task of using Python to programmatically calculate the ASCII values of 10 characters that she is going to give. He has to print the sum of the even ASCII values he computed as well.
Input Format:
The input will be given 20 space separated characters
A
B
C
D
E
F
G
H
I
J
Output Format:
The output will be a 11 integer values.
65 66 67 68 69 70 71 72 73 74 350
DSL Code Stub:
function(integer_array,AsciiProblem,character_array list_of_chars)
array(character, list_of_chars,10,single)
invoke(integer_array,result,AsciiProblem,list_of_chars)
print(integer_array,result)
Python Code Generate by above written DSL:
# Complete the 'AsciiProblem' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts CHARACTER_ARRAY list_of_chars as parameter.
#
def AsciiProblem(list_of_chars):
# Write your code here
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
list_of_chars = list(map(lambda x: x[0], input().rstrip().split()))
result = AsciiProblem(list_of_chars)
fptr.write(' '.join(map(str, result)))
fptr.write('\n')
fptr.close()
Note : The code Generated by the DSL code stub will only work in the Hackerrank console.