Question

In java6. Device Name System Suggested Problem Name: Device Name System Suggested Function Name: deviceNamesSystem...

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

6. Device Name System Suggested Problem Name: Device Name System Suggested Function Name: deviceNamesSystem Create unique dev

Sample Case 0 Sample Input STDIN Function devicenames[] size n = 4 devicenames=[mixer, toaster, mixer,tv] 4 mixer toa


12 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

//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;

}

Add a comment
Answer #2

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));

source: Solved on self
answered by: Ninad
Add a comment
Answer #3

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;

}


answered by: prathyush
Add a comment
Know the answer?
Add Answer to:
In java6. Device Name System Suggested Problem Name: Device Name System Suggested Function Name: deviceNamesSystem...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone code this asap? Use any language that you want. 2. Ancestral Names Given a...

    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...

  • Define the functions in Python 3.8 1. Write a function most frequent n that takes a...

    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...

  • For C++ This is the information about the meal class 2-1. (24 marks) Define and implement...

    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...

    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...

  • Complete the implementations of various classes in a Flight Management, We consider a database that stores...

    Complete the implementations of various classes in a Flight Management, We consider a database that stores information about passengers and flights. Each passenger record (e.g., String name, double TicketAmount, int flightID) can be uniquely identified by a String ID (e.g., ”e1”), whereas each flight record (e.g., String airline and String flight for flight name and airline name, respectively ) can be uniquely identified by an integer id. You must implement all methods in the FlightManagementSystem by manipulating the two attributes...

  • Objective In this assignment, you will practice solving a problem using object-oriented programming and specifically, you...

    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...

  • Develop a functional flowchart and then write a C++ program to solve the following problem. 1....

    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...

  • c++ problem You are asked to implement a car ordering system for Bobcats Auto Dealership. This...

    c++ problem You are asked to implement a car ordering system for Bobcats Auto Dealership. This dealership is brand new and only sells one brand of cars with three different models. However, a buyer can add options if they choose. Write a C++ program that allows the user to order a single car with different options. All the options available will be stored in a file called "options.txt" along with their cost. You may assume that the file will not...

  • Here is the code I made, but the test case is not working, it goes wrong...

    Here is the code I made, but the test case is not working, it goes wrong when the binary string convert to decimal. please help. #include "stdafx.h" #include <iostream> #include <string> #include <math.h> #include <locale> using namespace std; // function for option 1 void decToBin(int number) {        int array[16];        int i = 0;        for (int counter = 0; counter < 16; counter++)        {               array[counter] = 0;        }        while (number > 0)        {...

  • I need to do the following to the program below: Problem formulation Following previous assignment (development...

    I need to do the following to the program below: Problem formulation Following previous assignment (development of the Asset class) you now need to extend it and create a few derivative classes, i.e. a Router, a Switch, a Server, a PSU (Power Supply Unit) etc. Each of those new classes has to be declared as extending the base Asset class and introduce new attributes and methods. For example, you may consider the following skeletons or add something that matches your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT