Good day to all. Can you help find the length of the minimum word in the string
separated by spaces in java.
- oneSo in Java or Javascript? - Dex
- oh, quarrel =) on java. - Andrey2517
- @ Andrey2517, According to the rules of the forum, questions should not be reduced to the decision or the completion of student assignments. Please clarify what you have done yourself and what did not work out. - karmadro4
|
4 answers
Something like this in Java
String x="sdfds ds dsfsdf sdfs sdf sdf"; String[] xx=x.split(" "); int min=1000000; for (String i: xx){ if (i.length()>0 && i.length()<min) min=i.length(); } PS Mini Python Ads
x="sdfds ds dsfsdf sdfs sdf sdf" minlen=min(map(len, filter(lambda y: y!='', x.split(' ')))) - what to say, python is cool! - namak
- Python is a fucking programming language ... it's even more than just a language ..... - AseN
- @ReinRaus, explain popularly how the word NULL may appear after the split? . This is about i.length ()> 0. - avp
- oneTo be honest, I don’t remember the intricacies of individual languages, but for example Python: >>> x = 'sdfds ds dsfsdf sdfs sdf sdf' >>> x.split ("") ['sdfds', 'ds', 'dsfsdf' , '', 'sdfs', '', '', 'sdf', 'sdf'] It's easier to be safe, even when writing in Java or any other language. - ReinRaus
- oneSorry, wrong. Of course you need .split ("+"); - avp 8:36 pm
|
<script> function find_min(str) { str=str.replace(/,/g, ""); arr = str.split(' '); max_len = 0; for(i=0;i<=arr.length-1;i++) if(arr[i].length>max_len) {newstr=arr[i]; max_len=arr[i].length;} return newstr; } alert(find_min("Yesterday, all my troubffffffles seemed so far away Now it looks as though they're here to stay Oh, I believe in yesterday.")); </script> Pancake, so after all implementation on JS at first was necessary! Ugh)
- I apologize) my mistake - Andrey2517
- oneNothing, I'm used to it =) - AseN
|
IMHO, modern C # (3.0 and above) is not inferior to Python in beauty and expressiveness.
using System; using System.Linq; class Program { static void Main() { Console.WriteLine(Console.ReadLine().Split() .OrderBy(x => x.Length).LastOrDefault()); // можно еще так, вроде выглядит попроще, но работает медленнее Console.WriteLine(Console.ReadLine().Split().Max(x => x.Length)); } } |
Catch studonyata .. I wrote 20 minutes with the above example (laboratory), max and min number ..
package qq; import java.util.Scanner; public class alln { public static void main(String[] args){ Scanner sc = new Scanner(System.in); String x=sc.nextLine(); sc.close(); //String x="sdfds ds dsfsdf ds sdfs sdf sdf"; String[] xx=x.split(" "); int min = 1000000; int max = 0; int bufmin = 0; int bufmax = 0; String temp; String tempz; for(int i = 0; i < xx.length; i++) { temp = xx[i]; if (temp.length()<min){ min=temp.length(); bufmin=i; } } for(int i = 0; i < xx.length; i++) { tempz = xx[i]; if (tempz.length()>max){ max=tempz.length(); bufmax=i; } } System.out.print("Min: "+min+" ("+xx[bufmin]+") ; Max: "+max+"("+ xx [bufmax] +"); ");
} }
- oneI would put a deuce. 1. What happens if the array has a string longer than a million characters? 2. What happens if there is a zero-length string in the array? 3. Two passes! 4. Concatenation of strings! - VadimTukaev
- oneIt is always necessary to think. Especially when writing such small examples. Or did you think someone strongly interested in how you implement the principle itself? There's just a few options, and all have long been invented. I see a lot of code from people with such motivation (how to write faster). The truth is that in industrial programming is not particularly interested in speed. A professional programmer writes an average of 10 lines per day. The architecture of these lines is important, their "lickiness" to the ideal. Olimpiadniki in normal offices are not needed, they would prefer to hire a trainee "dont know" than a "know-it" with ambition - VadimTukaev
- 3> I say — I wrote in 20 minutes ---> The truth is that in industrial programming, speed is not that interesting to anyone. I don’t even know what drives me to sadness more - that someone thinks that twenty minutes for this task is not enough, or that someone thinks that the Olympiad is a chicken who has learned to quickly wave his paws, and in reality these skills are completely inapplicable. - etki
- 2However, I understood: I am saddened most of all by the fact that the issue is three years old. - etki
- oneDo not take it personally, I'm talking about trends. There is such a breed of "olympiadics" (the name is purely conditional, such a person could never take part in olympiads) who think that their ability to commit several hundred lines in a few tens of minutes (really saw) is needed by someone. "Che, it works!" And then he talks about the fact that everywhere there is a cronyism, everywhere a Jewish (Chechen, blue) mafia, so they don’t hire him. Business is simple and cruel: they do what is profitable. But to admit that the problem is that you are less profitable than a "dunno," so reluctance ... - VadimTukaev
|