Easy Python problem

If you consider yourself at all good at programming you should be able to answer this:

Write a function rem(c, s) that takes character c and a string s as inputs and removes the last and only the last occurrence of c.

Example:
>>rem('c','science')
'sciene'

Attached: csnoob.png (1600x490, 26K)

Other urls found in this thread:

kangax.github.io/compat-table/es6/
twitter.com/SFWRedditGifs

Do your own homework, fucker.

Read the string backwards and use a bool to determine if the character has been found already.

Then delete the character from the original string, and set the bool to true.

I'm not coding the problem for you.

...

string remc(char c, string s){
string a = s;
string b =" ";
while(a.find(c)!=string::npos)
{
b+= a.substr(0, a.find(c) + 1 );
a = a.substr(a.find(c) + 1, a.size() - a.find(c) - 1 );
}
b = b.substr(0, b.size() - 1) + a;
return b;
}
In a real language.
Reversing is for homos.

>Asks 4chin for homework help
Never gonna make it brah

Screw you guys, figured it out myself anyway

lmftfy
>string remc(char c, string s){
>>if(size_t n = s.find_last_of(c); n != string::npos){
>>>s.erase(n,1);
>>}
>>return s;
>}

>python
pathetic. try learning a REAL programming language like html

which programming language has the most aspie users?

all of them

anybody who needs to use code in their day to day life is inherently autistic

python is so bad you have to comment the type of c and s separately lmfao

not science or math

Haskell, Brainfuck, Whitespace

C

def rem(c,s):
return ''.join(s.rsplit(c, maxsplit=1))


Wow that was hard.

def rem(c,s):
s = list(s)
for charIndex in reversed(range(0, len(s))):
if (s[charIndex] == c):
del s[charIndex]
break
return ''.join(s)

... but it's shit compared to

Why you doin this?

Attached: lame_request.png (1366x768, 139K)

return s.replace(c,"")

Attached: 72fca01334ccf36fcbc486c023a551e7.jpg (690x1163, 249K)

def rem(c,s):
return s.reverse().replace(c,"").reverse()

Shoot, sorry.

return s[::-1].replace(c,"",1)

Attached: 35764191_p0_master1200.jpg (1000x707, 631K)

I'm an idiot

s[::-1].replace(c,"",1)[::-1]

(defun homework (c str)
(let ((next "")
(found nil))
(loop for x in (reverse (loop for i across str collect i)) do
(if (and (equal c x) (not found))
(setf found t)
(setf next (concatenate 'string next (string x)))))
(reverse next)))

(format t "~A~%" (homework #\c "science"))
[\code]

(defun rem (char string)
(remove char string :from-end t :count 1))

These are all inefficient. Shame on you

actually these are all pretty efficient so fuck you instead

def rem(char, string):
s = list(string)
for i in range(len(s)-1, -1, -1):
if s[i] == char:
s.pop(i)
break
# you didn't specify a return, but as strings are immutable it is necessary
return "".join(s)

re.sub(r"c(?!.*c)", "", s)

Attached: 1526588485206.png (469x452, 277K)

>breaking from an if statement
hello pajeet

ah shit, gj user.

I need to spend time learning the sl more

that just breaks from the nearest loop, which is the for-loop in line 3

def rem(c, s): return s[::-1].replace(c, '', 1)[::-1]


Python is stupid.

Attached: 1525087797-20180430after.png (360x360, 27K)

damn son

Fuck, you beat me to it.
lambda c,s: s[::-1].replace(c,"",1)[::-1]

python 2
def rem(c, s):
found = False
these = ''
for i in s[::-1]:
if not found and i == c:
found = True
else:
these = i + these
return these

print rem('c', 'science')

PHP because fuck you
function rem($c, $s)
{
$ec = preg_quote($c, '@');
return preg_replace("@^([^{$ec}]*){$ec}|{$ec}([^{$ec}]*)$@", "\\1\\2", $s);
}

echo rem('o', "Hello, OP. How the fuck are you doing today?\n");

Attached: pikakill.jpg (238x183, 12K)

t. brainlet
here, forgot a 1 as the second parameter to replace, sorry

module SomeFaggotsHomework where

rem :: Char -> String -> String
rem c s = take i s ++ drop (i + 1) s
where
i = index c s 0 0
index _ [] _ last = last
index c (s:ss) pos last
| c == s = index c ss (pos + 1) pos
| otherwise = index c ss (pos + 1) last


theres probably a much more efficient way to do this but i'm not that good at haskell

#include

public static void(){
do print("Hello xd c me c u c everyone u cuck.");
remove the last instance of 'c';
}
end

Fucking kill me.

Attached: big peepee froag.png (797x797, 324K)

>reversing a string twice
absolutly pythonic

func rem(_ c: Character, _ s: String) -> String {
var revS = String(s.characters.reversed())
if let revSIndex = revS.characters.index(of: c) {
revS.remove(at: revSIndex)
}
return String(revS.characters.reversed())
}

const rem=(c,s)=>{let i=s.lastIndexOf(c);return i

I know this is Javascript, but which "dialect?"

def rem(c, s):
for i in reverse(s):
if i == c:
s[-i].remove()

easy

oh, I typed that up in the reply box, assumed the spaces would stay. You get the idea.

wait, I fucked up:

def rem(c, s):
for x, i in enumerate(reverse(s)):
if i == c:
s[-x].remove()

just regular es6, supported everywhere
kangax.github.io/compat-table/es6/

guys what the fuck is this shit about, i literally did shit exactly as the tutorial said and it comes out wrong

really puts a nigga off trying to learn this shit ffs

Attached: 1526408903177.png (830x143, 13K)

strings don't equal ints.

The result of input() is a string.

So you need to either do:

if (num == '7'):

or
if (num == str(7)):

I meant to say

if (int(num) == 7):

for the second one.

BTW, you don't need brackets around the condition in Python, it should be:

if int(num) == 7:

def rem(c,s):
return s[::-1].replace(c,"",1)[::-1]

thanks it works, but if I don't input an integer it gives me an error instead of taking to the print command I want it to now.

i'd like it to be an integer between 1 and 10, BUT if you input text or an integer that's not within those parameters to give a different print instead of an error

also how do i make repeat code? like, making something run a specific line again instead of me having to restart?

If you call int() with a non integer you will get a ValueError, that is why you are getting an error.

You can catch the error with a try/except block and then print something to the user.

num = input("Input a number: ")
try:
num = int(num)
except ValueError:
print("This is not a number")
else:
if not 1

public String removeC(char a, String s){
String x = "";
for(int j = 0; j

Also, it's a good idea not to have integers strewn through code.
MAX = 10
MIN = 1
SUCCESS = 7

num = input("Input a number: ")
try:
num = int(num)
except ValueError:
print("This is not a number")
else:
if not MIN

>calling C++ a 'real language'
>not using C

fuck off

yeah man that's perfect, i really shouldn't be trying to go that deep since i literally just started but i did the EXACT same as the guy in the video and it didn't work which ticked me off

[Code]
Def rem(char,string):
(a,b,c) = string.rpartition(char)
Return a + c
[/code]

Attached: IMG_20180526_163604.jpg (639x626, 51K)

hello brainlet try reading it again

>pajeets calling others pajeets

kotlin master race
fun rem(c: Char, s: String) = s.removeAt(s.indexOfLast { it == c })

i can do this in TI Basic, can I join anonymouse now plx?

Holy shit, everyone is massively overcomplicating this.

def rem(c, instr):
last_idx = instr.rfind(c)
# Ignore next two lines if it's safe to assume the input always contains c
if (last_idx == -1):
return instr
return instr[:last_idx] + instr[last_idx+1:]


5 lines, 3 if the precondition check isn't needed. The only objects allocated past the initial arguments are one integer, two intermediate strings, and the result string.

Too long and horribly inefficient. An explicit loop will be slower than a behind-the-scenes C function. Not to mention list(string) means a string is getting allocated for every list element.

Inefficient. Allocates two new strings plus an array, then the final string.

Bad, there's no need to reverse a list just to do right-indexing.

Same problems as #1.

Good and works in any language with a sane regex system. Whether it's the best performance depends on the language.

See others.

See me after class. Worst of the bunch. Every time the string gets appended in the loop, a new string object is allocated.

t. pajeet
What the fuck is this even supposed to be?
>s[-x].remove()
OOP in loo

Actually most java pajeets are smart enough to not make basic mistakes like string concatenation in a loop, even if it's only because their IDE tells them to stop fucking doing it.

I did it, what do I win?
>>> def rem(c, s):
i = s.rfind(c)
return s[:i] + s[i+1:] if i != -1 else s

>>> print(rem('c','science'))
sciene
>>> print(rem('e','science'))
scienc
>>> print(rem('f','science'))
science

char *rem(char *str, char c)
{
char *ret = malloc(strlen(str));
char *occ = strrchr(str, c);
strcpy(ret, str);
strcpy(ret + (occ - str), occ + 1);
return ret;
}

>See others.
Counterpoint: one-liners are chad solutions.

def rem(string,c):
return string[:len(string) - string[::-1].find(c)-1] + string[len(string) - string[::-1].find(c):]

no string.h
#include
#include

char *rem(char c, const char *s)
{
char *t;
char *str;
int len = 0;
const char *p;
const char *idx = NULL;
for (p = s; *p != '\0' && ++len; p++)
if (*p == c) idx = p;
t = str = (idx == NULL) ? malloc(len + 1) : malloc(len);
for (p = s; *p != '\0' && p != idx; p++)
*t++ = *p;
if (*p != '\0') for (++p; *p != '\0'; p++)
*t++ = *p;
*t = '\0';
return str;
}

int main()
{
char *s;
printf("%s\n", s = rem('c', "science"));
free(s);
printf("%s\n", s = rem('e', "science"));
free(s);
printf("%s\n", s = rem('f', "science"));
free(s);
return 0;
}