Posts

Showing posts with the label string

Space complexity of adding Java Strings char by char

Space complexity of adding Java Strings char by char When we have to build a Java String char by char through a loop through "addition", it can be observed that the time complexity of performing this operation is poor: it is of quadratic O(n^2) time. However, I am wondering if the space complexity of "adding" a String char-by-char through a loop is also poor. O(n^2) Here is a minimal example of a program which performs building a String via char-by-char addition with stopwatch timing. The results I got shown below clearly show a quadratic curve: /**Create a String of n 'A's char-by-char. * @param n Number of 'A's to be appended * @return The finished string */ public static String appendString(int n) { String str = ""; // Timed run starts here long t = System.currentTimeMillis(); // String concatenation occurs here for (int k = 0; k < n; k++) str += "A"; t = System.currentTimeMillis() - t; ...

arrangement of names with same starting alphabets

arrangement of names with same starting alphabets public class solution{ public static void main(String args){ String priya={"priya","nandhni","nithesh","varan","rekha","sri"}; System.out.println(priya); int n=priya.length; for(int i=0,j=i+1;i<n;i++){ if(priya[i].compareTo(priya[j])>0) { String temp=priya[i]; priya[i]=priya[j]; priya[j]=temp; } } for(int i=0;i<n;i++){ System.out.println(priya[i]); } } } The output for ascending order comes as nandhni varan nithesh priya rekha sri. what could the mistake here? Your code is not a complete sort. You only consider directly adjacent pairs of names once. – Elliott Frisch Jul 1 at 17:28 ...

Python how to include a string with random integer [duplicate]

Python how to include a string with random integer [duplicate] This question already has an answer here: I want to make a random police unit generator and the code is like so: import random unitnumbers = ('unit' , random.randint(1,999)) print(unitnumbers) However, rather than printing "unit 63" , it prints ('unit', 378) How could I make it print only the unit and random number? This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. 2 Answers 2 You are making the value of unitnumbers a tuple. You should instead make it a string like that: unitnumbers unitnumbers = "unit " + str(random.randint(1,999)) This should do that: import random unitnumbers = 'unit %d' % random.randint(1,999) print(unitnumbers)

How to find the first character of one dimensional string in java

How to find the first character of one dimensional string in java public class solution{ public static void main(String args){ String priya={"priya","nandhni","nithesh","varan","rekha","sri"}; for(int i=0;i<priya.length;i++) { System.out.println(priya.charAt(i)); } } } This is not working.How can I separate first letters p, n, n, v, r, s to a char array from the above string using java? Have you tried some Java code yet? – Tim Biegeleisen Jul 1 at 15:02 Use String.charAt(index) to access the character in String – Mạnh Quyết Nguyễn Jul 1 at 15:03 String.charAt(index) ...

Replace number in word

Replace number in word How is it possible to replace every 1 with one , every 2 with two , every 3 with three ... from an Input? 1 one 2 two 3 three My Code: import javax.swing.JOptionPane; public class Main { public static void main(String args) { String Input = JOptionPane.showInputDialog("Text:"); String Output; //replace Output = Input.replaceAll("1", "one"); Output = Input.replaceAll("2", "two"); //Output System.out.println(Output); } } It just work with one replace-item. What is the context for the replacements? Do you want to really replace every digit 1 with one , even if the former appears inside a large number, e.g. 123 ? – Tim Biegeleisen Jul 1 at 15:09 1 one 123 No. It schut work li...

Trying to copy back elements in Array works with integers but not with strings in Multiprocessing Python module

Trying to copy back elements in Array works with integers but not with strings in Multiprocessing Python module I am trying to write a process which does some computation on an Array filled with strings using the multiprocessing module. However, I am not able to get back the results. This is just a minimalist code example: from multiprocessing import Process, Value, Array from ctypes import c_char_p # Process def f(n, a): for i in range(0,10): a[i] = "test2".encode('latin-1') if __name__ == '__main__': # Set up array arr = Array(c_char_p, range(10)) # Fill it with values for i in range(0,10): arr[i] = "test".encode('latin-1') x = for i in range(0,10): num = Value('d', float(i)*F) p = Process(target=f, args=(num, arr,)) x.append(p) p.start() for p in x: p.join() # THis works print(num.value) # This will not give out anything print(ar...

What is the cause of my IndexOutOfBoundsException

What is the cause of my IndexOutOfBoundsException I am trying to upload a file using primefaces and hibernate. My managed bean has the following function: public void upload(){ if(file != null){ try { FacesContext context = FacesContext.getCurrentInstance(); ServletContext servletContext = (ServletContext) context.getExternalContext().getContext(); String dbPath = servletContext.getRealPath("/"); String webcut = dbPath.substring(0, dbPath.lastIndexOf("\")); String buildcut = webcut.substring(0, webcut.lastIndexOf("\")); String mainURLPath = buildcut.substring(0, buildcut.lastIndexOf("\")); InputStream inputStream = file.getInputstream(); String path = mainURLPath + "\web\resources\images" + file.getFileName(); File destFile = new File(path); if(!destFile.exists()){ FileUtils.copyInputStreamT...

How to check if an item in an ArrayList does not contain a certain word?

How to check if an item in an ArrayList<String> does not contain a certain word? I want to use a for loop like this: `button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Object.setList(); for(int i = 0; i < Object.getList.size(); i++) if(!Object.getList.get(i).trim().toLowerCase().contains("hello")) { Toast.makeText(getContext(), "List does not contain "Hello", Toast.LENGTH_LONG).show; } }` This is my model: public class Object { private ArrayList<String> mArrayList; public void setList() { mArrayList = new Arraylist<>(); mArrayList.add("Random Message"); mArrayList.add("ZZZZ") } public ArrayList<String> getList() { return mArrayList; } } I still keep getting the Toast message, but it works just fine when i use: if(!Object.getList().toString.trim().toLowerCase().contains("hello")) ...