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

  • Problem 1 In this problem, you will write two functions. The first function takes in a...

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

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

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

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

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

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

  • // READ BEFORE YOU START: // You are given a partially completed program that creates a...

    // READ BEFORE YOU START: // You are given a partially completed program that creates a list of students for a school. // Each student has the corresponding information: name, gender, class, standard, and roll_number. // To begin, you should trace through the given code and understand how it works. // Please read the instructions above each required function and follow the directions carefully. // If you modify any of the given code, the return types, or the parameters, you...

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