In java
6. Device Name System
Suggested Problem Name: Device Name System
Suggested Function Name: deviceNamesSystem
Create unique device names to be used in a residential loT (Internet of Things) system. If a device name already exists in the system, an integer number is added at the end of the name to make it unique. The integer added starts with 1 and is incremented by 1 for each new request of an existing device name. Given a list of device name requests, process all requests and return an array of the corresponding unique device names.
Example
n=6
devicenames = ['switch', 'tv', 'switch', 'tv', 'switch', 'tv']
Function Description
Complete the function deviceNamesSystem in the editor below.
deviceNamesSystem has the following parameter(s):
string devicenames/n]: an array of device name strings in the order requested.
Returns
string [n] : an array of string usernames in the order assigned
//required method, copy and paste wherever you want
public static String[] deviceNamesSystem(String deviceNames[]) {
// creating an array of same size to keep the results
String uniqueNames[] = new String[deviceNames.length];
// looping from i=0 to deviceNames.length-1
for (int i = 0; i < deviceNames.length; i++) {
// fetching device name at index i
String name = deviceNames[i];
// initially assuming this is not a duplicate value
boolean duplicate = false;
// looping and checking if this value exist previously
for (int j = 0; j < i; j++) {
if (uniqueNames[j].equals(name)) {
// exists. setting duplicate to true and exiting inner loop
duplicate = true;
break;
}
}
// if this is no duplicate, adding unchanged name to uniqueNames
// array
if (!duplicate) {
uniqueNames[i] = name;
} else {
// otherwise starting with id=1
int id = 1;
boolean exists = true;
// looping as long as exists is true
while (exists) {
// initially assuming device with this name does not exist
// already
exists = false;
// looping and checking the previous elements in uniqueNames
// for devicename: name+id (here + is for concatenation)
for (int j = 0; j < i; j++) {
if (uniqueNames[j].equals(name + id)) {
// found, setting exists to true
exists = true;
// moving to next id
id++;
// exiting inner loop
break;
}
}
}
// after the loop, adding name+id to the uniqueNames array
// example: if name is "switch", and id is 7, name+id returns
// "switch7"
uniqueNames[i] = name + id;
}
}
//returning the array
return uniqueNames;
}
public static List<String> deviceNamesSystem(List<String> devicenames) {
List<String> res=new ArrayList<>();
HashMap<String,Integer> map=new HashMap<>();
for(int i=0;i<devicenames.size();i++){
if(map.containsKey(devicenames.get(i))){
String temp=devicenames.get(i)+""+map.get(devicenames.get(i));
res.add(temp);
map.put(devicenames.get(i), map.get(devicenames.get(i)) + 1);
}
else{
res.add(devicenames.get(i));
map.put(devicenames.get(i), 1);
}
}
return res;
}
String a[] = {"a","a","b","c","a","d","b","b","b"};
String b[] = new String[a.length];
int cnt = 0;
for (int i = 0; i < a.length; i++) {
cnt = 0;
for (int j = 0; j < a.length; j++) {
if(a[i] == a[j]) {
cnt++;
if(cnt-1 == 0) {
b[j] = a[i];
}else {
b[j] = a[i]+ (cnt-1);
}
}
}
}
System.out.println(Arrays.asList(b));
In java6. Device Name System Suggested Problem Name: Device Name System Suggested Function Name: deviceNamesSystem...
Can someone code this asap? Use any language that you want. 2. Ancestral Names Given a list of strings comprised of a name and a Roman numeral, sort the list first by name, then by decimal value of the Roman numeral. In Roman numerals, a value is not repeated more than three times. At that point, a smaller value precedes a larger value to indicate subtraction. For example, the letter I represents the number 1, and Vrepresents 5. Reason through...
Problem 1 In this problem, you will write two functions. The first function takes in a string and returns that string without any dashes. The second function takes in the first and last name and returns a string that is lastname_firstname and uses the previous function to remove any dashes (-) in the name. Note that you’ll be testing it by calling it from the command line; you’ll call it from a script in problem 3. Deliverables: Function file that...
Define the functions in Python 3.8 1. Write a function most frequent n that takes a list of strings and an integer n, and that returns a dictionary where the keys are the top n most frequent unique words in the list, and the values are the frequency of each word: For example, most frequent n(text, 3) should return the dictionary {'is': 2, "the’: 3, 'of': 2}, and most frequent n(text, 2) could return either {'is': 2, 'the’: 3} or...
0. Introduction. This involves designing a perfect hash function for a small set of strings. It demonstrates that if the set of possible keys is small, then a perfect hash function need not be hard to design, or hard to understand. 1. Theory. A hash table is an array that associates keys with values. A hash function takes a key as its argument, and returns an index in the array. The object that appears at the index is the key’s...
You need to program a simple book library system. There are three java classes Book.java // book object class Library.java //library class A2.java //for testing The Book.java class represents book objects which contain the following fields title: a string which represents the book title. author: a string to hold the book author name year: book publication year isbn: a string of 10 numeric numbers. The book class will have also 3 constructors. -The default no argument constructor - A constructor...
For C++ This is the information about the meal class 2-1. (24 marks) Define and implement a class named Dessert, which represents a special kind of Meal. It is to be defined by inheriting from the Meal class. The Dessert class has the following constructor: Dessert (string n, int co) // creates a meal with name n, whose type // is "dessert" and cost is co The class must have a private static attribute static int nextID ; which is...
Stuck on this computer science assignment Write a program that demonstrates binary searching through an array of strings and finding specific values in an corresponding parallel double array. In all cases your output should exactly match the provided solution.o. Provided files: Assignment.cpp - starter assignment with function prototypes companies.txt - file for program to read. earnings.txt - file for program to read. Input1.txt Input2.txt - Test these inputs out manually, or use stream redirection Input3.txt - These inputs are based...
Objective In this assignment, you will practice solving a problem using object-oriented programming and specifically, you will use the concept of object aggregation (i.e., has-a relationship between objects). You will implement a Java application, called MovieApplication that could be used in the movie industry. You are asked to implement three classes: Movie, Distributor, and MovieDriver. Each of these classes is described below. Problem Description The Movie class represents a movie and has the following attributes: name (of type String), directorName...
// CSE240 Spring 2019 HW 7 & 8 // Write your name here // Write the compiler used: Visual studio or gcc // READ BEFORE YOU START: // You are given a partially completed program that creates a linked list of patient information. // The global linked list 'list' is a list of patients with each node being struct 'patientList'. // 'patientList' consists of struct 'patient' which has: patient name, room number, and a linked list of 'doctors'. // The...
Develop a functional flowchart and then write a C++ program to solve the following problem. 1. Create a text file named c1.txt and write your brand of computer (like Dell, HP, etc) in the file. You will be reading the name of the file from the keyboard as a string, using the string class. Your program will also read the brand of your computer from the keyboard. The process of the file creation (name of the file, mode for opening...