Skip to main content
15 events
when toggle format what by license comment
Oct 28, 2022 at 11:13 comment added Ismael Miguel Also, outputting by putting the value into a regular variable is disallowed. And I fixed the bug for you: for($T=$s=$argv[1];$argv[3]--&&$R=!1!==$c=strrpos($s,$x=$argv[2]);)$s=substr($s,0,$c).substr($s,$c+strlen($x));echo$R?$s:$T; <-- 124 bytes. This will print "Cats do - while lions do not ." for 3 but "Cats do meow-meow while lions do not meow." for 4, for the existing test case, in your link.
Oct 28, 2022 at 10:59 comment added Ismael Miguel You can use !1 instead of false, saving you 3 bytes. You can save 3 more bytes by saving $argv[2] into a temporary variable. This is the final solution: for($s=$argv[1];$argv[3]--&&!1!==$c=strrpos($s,$x=$argv[2]);)$s=substr($s,0,$c).substr($s,$c+strlen($x));. But currently, if you change the 3rd argument to 4, it doesn't show the original string.
Oct 27, 2022 at 17:51 comment added Ismael Miguel @Ratchet2277 Currently, you have a solution that doesn't work. Using an array with array_unshift() and removing elements from it, when a join() for the remaining elements will get you there pretty quickly. It might even be shorter than what you currently have, and will always work.
Oct 27, 2022 at 17:21 comment added Ratchet2277 Hum, spliting into array sounds like a good idea for lisibility/conviniance, but i got some doupt about interest here @IsmaelMiguel since the goal is more to make code shorter, and array operation function aren't that short too, it sill depend on seperator used i guess, maybe with some array_unshift()? I gonna looking for that anyway so thanks for the idea ^^'
Oct 27, 2022 at 17:18 history edited Ratchet2277 CC BY-SA 4.0
added 29 characters in body
Oct 26, 2022 at 11:27 comment added Ismael Miguel It might be easier to split the string into multiple substrings, and then work with that new array. This substring method will always fail in the case that JvdV said.
Oct 26, 2022 at 7:46 comment added JvdV Just a headsup: The answer is not correct. OP now clarified that Cats do meow not memeowow, they do meow-ing where n=3 should return Cats do not meow, they do -ing.
Oct 26, 2022 at 1:38 comment added naffetS BTW, it's actually shorter to reference substr twice than storing it in a variable, even without the quotes.
Oct 26, 2022 at 1:37 comment added naffetS However, by our consensus, we can't take input/output via a variable. You can try taking input via ARGV, though. That's often shorter than a function
Oct 26, 2022 at 1:36 comment added naffetS You don't need the {}.
Oct 25, 2022 at 18:38 comment added Ismael Miguel The problem is that, on PHP 8.0+, it throws an exception for all constants that aren't defined. In this case, $t="substr" works fine in newer versions, but $t=substr gives you a "Fatal error: Uncaught Error: Undefined constant "substr" in [...]".
Oct 25, 2022 at 17:48 comment added Ratchet2277 Don't even need to downgrade PHP version, juste disable error reporting do the job here, thx for the tip. I still need to fix that bug without adding to much bytes so ^^'
Oct 25, 2022 at 14:23 comment added Ismael Miguel Also, you have a bug. The rules say: "If the substring is not present in the string or the number of times the substring appears is less than n, then the output is the original string.". For an n = 5, on test case 0, it displays "not , they do -ing.".
Oct 25, 2022 at 14:18 comment added Ismael Miguel If you don't mind using older versions, like 5.3-5.6 or some 7.x versions, you can reduce by 2 bytes by removing the double quotes.
Oct 25, 2022 at 13:54 history answered Ratchet2277 CC BY-SA 4.0