Write a function that takes two strings as arguments. The function should return
ID: 3863211 • Letter: W
Question
Write a function that takes two strings as arguments. The function should return the number of times the second argument appears in the first. Write a function that takes a list and an integer argument. The function should return a new list with each of the values of the argument list multiplied by the value of the integer argument. Write a program that uses a dictionary to keep track of the grades associated with students. The keys of the dictionary should be student IDs and the values should be a list of grades. The user should be able to repeatedly specify a student number and the grade to add. When the user enters "quit" for the student number, print out the average grade for each student that had at least 1 grade entered. Write a function that takes two string arguments. The function should return a new string value that is equivalent to the first argument with all of the occurrences of the second argument removed. For example, if the inputs are "My catdog's breath smells like catdog food" and "dog", the returned value should be "My cat's breath smells like cat food".Explanation / Answer
since it is not written in which language one should answer so i am using java
Q2
public static int findOccurences(String str, String str_toFind){
int lastIndex = 0;
int count = 0;
while (lastIndex != -1) {
lastIndex = str.indexOf(str_toFind, lastIndex);
if (lastIndex != -1) {
count++;
lastIndex += str_toFind.length();
}
}
return count;
}
Q3
public static List<Integer> MultiplyList(List<Integer> list, int number_to_multiply){
int val=0;
for(int i=0;i<list.size();i++){
val=0;
val = list.get(i);
val =val * number_to_multiply;
list.set(i, val);
}
return list;
}
Q5
public static String RemoveOccurences(String str1,String str2){
String str_to_Return = "";
String str[] = str1.split(str2); // Splitting the string str1 whereever string str2 is present
for(int i=0;i<str.length;i++)
str_to_Return = str_to_Return+str[i]; and then concatenating each string of array
return str_to_Return;
}
Q4
public static void getStudentGrades(){
Scanner sc = new Scanner(System.in);
String ch = "" ;
String name="";
HashMap<String,ArrayList<String>> studentRecords = new HashMap<String,ArrayList<String>>();
do{
ArrayList<String> grades = new ArrayList<String>();
System.out.println("Enter Student number");
name = sc.next();
System.out.println("Enter student grades seperated Q to exit");
do{
String grade = sc.next();
if(grade.equals("Q"))
break;
else
grades.add(grade);
}while(true);
studentRecords.put(name,grades);
System.out.println("want to add more records (Y to continue) ");
ch = sc.next();
}while("Y" .equals(ch));
Set<String> keyset = studentRecords.keySet();
System.out.println("Displaying student records:");
for(String key: keyset){
System.out.print("Name: "+ key+ " Grades "+studentRecords.get(key)+" ");
}
}
to test the whole code please find the test Class which will test all the function and print the data
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
public class demo12 {
public static void main(String args[]){
int num=3;
List<Integer> a = new ArrayList<Integer>();
a.add(1);
a.add(2);
a.add(3);
System.out.println("original list is ");
for(int i=0;i<a.size();i++)
System.out.print(a.get(i)+" ");
a = MultiplyList(a,num);
System.out.println();
System.out.println("final list after multiplication of each element with "+num+" is");
for(int i=0;i<a.size();i++){
System.out.print(a.get(i)+" ");
}
System.out.println();
String str1 = "My Catdogs'breath smells like catdogs food";
String str2 = "dog";
int occurences = findOccurences(str1,str2);
System.out.println("occurences of "+ str2 + " in "+str1+ " is : "+occurences);
System.out.println("orginal string is --> "+str1);
System.out.println("string after removing "+str2 + " from " +str1 +" is" );
String str = RemoveOccurences("My Catdogs'breath smells like catdogs food","dog");
System.out.println(str);
getStudentGrades();
}
public static int findOccurences(String str, String str_toFind){
int lastIndex = 0;
int count = 0;
while (lastIndex != -1) {
lastIndex = str.indexOf(str_toFind, lastIndex);
if (lastIndex != -1) {
count++;
lastIndex += str_toFind.length();
}
}
return count;
}
public static List<Integer> MultiplyList(List<Integer> list, int number_to_multiply){
int val=0;
for(int i=0;i<list.size();i++){
val=0;
val = list.get(i);
val =val * number_to_multiply;
list.set(i, val);
}
return list;
}
public static String RemoveOccurences(String str1,String str2){
String str_to_Return = "";
String str[] = str1.split(str2);
for(int i=0;i<str.length;i++)
str_to_Return = str_to_Return+str[i];
return str_to_Return;
}
public static void getStudentGrades(){
Scanner sc = new Scanner(System.in);
String ch = "" ;
String name="";
HashMap<String,ArrayList<String>> studentRecords = new HashMap<String,ArrayList<String>>();
do{
ArrayList<String> grades = new ArrayList<String>();
System.out.println("Enter Student number");
name = sc.next();
System.out.println("Enter student grades seperated Q to exit");
do{
String grade = sc.next();
if(grade.equals("Q"))
break;
else
grades.add(grade);
}while(true);
studentRecords.put(name,grades);
System.out.println("want to add more records (Y to continue) ");
ch = sc.next();
}while("Y" .equals(ch));
Set<String> keyset = studentRecords.keySet();
System.out.println("Displaying student records:");
for(String key: keyset){
System.out.print("Name: "+ key+ " Grades "+studentRecords.get(key)+" ");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.