(Scheme Programming): Define a function that takes a list of integer numbers and
ID: 3754684 • Letter: #
Question
(Scheme Programming):
Define a function that takes a list of integer numbers and returns the processed version of the list. For processing, only one of the ff. should be done to each number in the given list:
If the number is a multiple of 3, replace by “T” | • If the number is a multiple of 5, replace by “ICE" • If the number is a multiple of both 3 and 5, replace by “T-ICE"
If not any of the first three, keep the same number
Note: No other function(s) are allowed to be defined. No let or lamda allowed. No identifiers are allowed to be defined.
The list to be used will be typed in the interactive pane.
Input: (T-ICE `(1 3 5 15 7))
Output: (list 1 "T" "ICE" "T-ICE" 7)
Explanation / Answer
//Java Program for above problem
//Copy and Paste in Java IDE and run with class name TICE
import java.util.Scanner;
public class TICE {
//function which returns list of String after processing
String[] TICES(int[] list)
{
String ans[]=new String[list.length];
for(int i=0;i<list.length;i++)
{
if(list[i]%3==0&&list[i]%5!=0)
{
ans[i]="T";
}
else if(list[i]%5==0&&list[i]%3!=0)
{
ans[i]="ICE";
}
else if(list[i]%3==0&&list[i]%5==0)
{
ans[i]="T-ICE";
}
else
{
ans[i]=Integer.toString(list[i]);
}
}
return ans;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//TO take input of the list at prompt by the user
Scanner scan=new Scanner(System.in);
System.out.println("Enter length of the String");
int n=scan.nextInt();
TICE t=new TICE();
int list[]= new int[n];
for (int i=0;i<n;i++)
{
list[i]=scan.nextInt();
}
String ans[]=t.TICES(list);
for (String element: ans) {
System.out.println(element);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.