[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Checking if a string is the same character repeated. Does python

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 54
Thread images: 5

File: ??ALLU.jpg (188KB, 500x500px) Image search: [Google]
??ALLU.jpg
188KB, 500x500px
Checking if a string is the same character repeated. Does python do it best?

s='aaaaaaaa'
s == s[0] * len(s)
True
>>
int is_repeated(const char *s)
{
unsigned i;
for (i = 1; s[i]; i++)
if (s[i - 1] != s[i])
break;
return (i == strlen(s));
}
>>
s == filter (==head s) s
>>
>>57894235

len(set(s)) == 1
>>
Any language with RegEx does it best.
>>
>>57894581
Look I know you're a nigger but compiling a regex statement for such tasks will not be faster.
>>
     String strTest;
String strNotTest;
char c1;
char c2;
strTest = kb.nextLine ();
strNotTest = kb.nextLine();
/*assuming you're declaring a scanner to allow any values to. be assigned*/
c1 = strTest.charAt (0);
c2 = strNotTest.charAt (0);
if (c1 == c2)
{
SOP ("They're equal");
}
else
{
SOP ("They're not equal");
}
>>
File: 1472819362562.jpg (59KB, 655x527px) Image search: [Google]
1472819362562.jpg
59KB, 655x527px
>>57894639
Never mind, thought we were just checking for the first character, it appears I'm illiterate.
>>
>>57894532
Scientifically speaking what's the complexity here?
>>
>>57894235
>exception if string is empty

>>57894466
>segfaults if empty string
>only works on null terminated strings

>>57894532
>pretty neat, but not sure about speed or behavior

>>57894581
>regex probably will carry some extra baggage for such a simple operation

>>57894639
>exception for empty strings
>java
>>
File: 1477442239364.jpg (65KB, 680x680px) Image search: [Google]
1477442239364.jpg
65KB, 680x680px
>>57894235
JS, just because
var matchingString = 'aaaaaa';
var nonMatchingString = 'aaabaa';
!!matchingString.split('').reduce((a, b) => {return a===b?a:undefined}) //true
!!nonMatchingString.split('').reduce((a, b) => {return a===b?a:undefined}) //false
>>
>>57894754
System.out.print ("don't enter an empty string please");
>>
return string.chars().distinct().count() == 1;


:^)
>>
>>57894794
to the fucking oven
>>
You could just check by looking at it with your eyes
>>
>>57894611
Look I know you're a retard but who said anything about speed.
>>
>>57894754
>>only works on null terminated strings
you mean it only works on strings?
>>
>>57894754
>only works on null terminated strings
u fucking wot
>>
>>57894532
this. the complexity is O(1). creating a set out of a container is O(1) and getting its length is O(1).

i have no idea what the OP is trying to do, but it reads like he made up some contorted logic to a pretty simple problem.
>>
>>57894839
My code creates a string the same length as the provided str with the first character. If the strings match then the two are the same
>>
>>57894830
>>57894812
some languages do not null terminate
but more specificly, c++ strings do lazy termination and so you are not guarenteed to have it null terminated
>>
>>57894852
so you generate a whole second string? do you have any idea how stupid that is?
>>
>>57894235
>>57894466
>>57894855

bool strRep(std::string str){
if(str.length()==0) return false;
char a=str[0];
for(int i=1; i<str.length(); i++){
if(str[i] != a) return false;
}
return true;
}
>>
>>57894918
>
if(str.length()==0) return false;

if (str.empty()) return false;
>>
>>57894968
damn, you got me on that one
>+1 for readability
>>
>>57894802
But look at how efficient it is! Only one simple line of code. Those C manchildren use all sorts of heavy stuff like loops for such a simple task.
>>
>>57894235
You're trash and your language is trash:

s='ééééé'
s == s[0] * len(s)
False


>>57894466
>>57894532
>>57894639
>>57894794
>>57894918
You too.
>>
>>57894974
i'll do ya one better
bool strRep(std::string str)
{
if (str.empty()) return false;
char c1 = str[0];
return std::all_of(str.begin(), str.end(), [&c1](char c) { return c1 == c; });
}
>>
>>57895108

Works for me - python3.5
>>
>>57895152
That's an improvement at least, but it still fails for combining diacritics
>>
>>57895199
Probably because combining diacritics are their own characters and strings with them should fail the test
>>
>>57895229
How about nigger emoji or country flags?
>>
>>57894531
note that lazy eval here prevents whole string from being evaluated if it mismatches early. works on infinite strings if not true. easy to read. quick to write.

minus points for exception on empty list.
>>
>>57895112
i still have not used a lambda in c++
python was the only time, but that was very rare
i usually still just made a function because i almost never had one-liners to do a job
>>
>>57894466
>index
>strlen when size is irrelevant
git gud

int is_repeated(const char *s)
{
while (*++s)
if (s[-1] != *s)
return 0;
return 1;
}
>>
>>57894807
speed is always fucking relevant, rajeet
>>
>>57894690
o(n) time and memory complexity
>>
>>57894807
found the app developer
>>
I'm kinda disappointed. Apparently hiding the type on argv makes it not be a char**?
main(int c,char **v){int b=0;char*a=*++v;c=*a;for(;*++a;)b|=*a!=c;b?puts("not equal"):puts("equal");}
>>
>>57896982
>b?puts("not equal"):puts("equal");
You missed a pretty big savings opportunity here.
printf("%sequal",b?"not ":"");
>>
>>57896982
The other day I saw a redditor try to pass off a 372 character C one-liner in a weak attempt at codegolf.

Removing all the whitespace is NOT code golf.
>>
>>57894235
Python has godtier string parsing, yes.
>>
>>57894235
This is what I hate about python.
You have no fucking idea what the interpreter is doing in the background, what kind of algorithm its using, how much memory, etc.

To know all of that, you have to read the interpreter reference manual.
And if you're going that far, you might as well invest the effort into learning a real language.
>>
Paste this into python IDLE.
http://pastebin.com/raw/WWCgAvXb
>>
how many of you use IDLE?
is it worth using or should I change to a text editor like VS: Code?.
>>
>>57894235
>`date +%Y`
>anything but null-terminated char arrays
int func(char *s) 
{
while (*s && *s == *++s);
return !*s;
}
>>
>>57897851
PyCharm

>>57895108
use the right kind of strings fagot
s=u'ééééé'
s == s[0] * len(s)
True
>>
>>57894235
Showing her hair in public is absolutely haram. Reported to the ISIS HQ.
>>
File: 1466739221440.jpg (39KB, 343x432px) Image search: [Google]
1466739221440.jpg
39KB, 343x432px
>>57897944
Neat.
>>
>>57897944
very well done
>>
>>57894235
terribly inefficient.
>>
>>57894754
>>only works on null terminated strings
A string without a null terminator is not a string. In C anyway, and that code sample is quite clearly written in C.
>>
>>57894855
>c++ strings do lazy termination and so you are not guarenteed to have it null terminated
uhhhh, he's passing in a const char*. to get a const char* from an std::string you have to call std::string::c_str() which null terminates it you fucking retard.
>>
>>57894794
>using the smiley with a carat nose
Thread posts: 54
Thread images: 5


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.