2 Replies - 233 Views - Last Post: 18 February 2014 - 08:08 PM Rate Topic: -----

#1 haven_v  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-February 14

Defining A Function

Posted 18 February 2014 - 04:11 PM

Hello!

My name is Haven. I'm brand new here and to programming in general. I'm currently a physics major at a major university and it's imperative that I understand programming so that I can be involved in research labs.

I'm currently in an intro level python course here at my uni. I have a project that I've been trying to figure out for literally days, and I have one problem that's been stumping me. Since none of my professors are emailing me back (I had to take this course online), I thought I'd get involved here to help myself not only with this problem but to also be able to help others with code/ learn things I don't understand. As I said this is for homework, so I'd like to understand the concept that I'm missing and how to implement it, but I don't want the answer explicitly given to me please. Understanding is far more important to my career than the grade.


So in this project we are writing our own functions that basically already exist in Python, and the idea is to create a library and a test file and submit both. 1 of the 8 functions we have to create is find_str, which is supposed to do the same thing as the string method "find". I already created a find_chr which only works for two parameters, a string and a single character, and that works just fine. However for find_str, I still need two parameters but instead of my second being a single character, it will be a portion of the original string that is more than 1 character. It should return the index number of the first character in the string, at it's lowest occurrence.

Here is my code:
# find_str definition

def find_str(s, wrd):
    x = len(wrd)
    n = 0
    while n <= len(s) - len(wrd):
        y = s[n:x+n]
        n +=1
        if y == wrd:
            w = y[0]
            return(find_chr(s, w))
        
    else:
        result = -1
        return result


For the record, find_chr finds the index # of just one character. It is in my library from which I'll be running this code.

So this code works fine, except for the obvious thing I'm sure you noticed which is that clearly plugging for y[0] is not very practical.
Take the example find_str("hello", "lo")
y[0] = l, however when I try to find the index # just l in the string "hello", clearly I will be returned with the index 2, when in reality my second parameter begins at index 3. For more complicated examples, this is an even bigger issue.

I also have a replace_str function that I can't yet write because in order to replace a string, I have to be able to isolate it successfully first. I seem to be missing this concept.

Any help would be very appreciated. I look forward to hopefully participating pretty actively here.

This post has been edited by andrewsw: 18 February 2014 - 04:14 PM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Defining A Function

#2 DK3250  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 105
  • Joined: 27-December 13

Re: Defining A Function

Posted 18 February 2014 - 04:40 PM

When y==wrd you have a known value of n - try see if you can use it...
Was This Post Helpful? 1
  • +
  • -

#3 haven_v  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-February 14

Re: Defining A Function

Posted 18 February 2014 - 08:08 PM

View PostDK3250, on 18 February 2014 - 04:40 PM, said:

When y==wrd you have a known value of n - try see if you can use it...


AHHHH figured it out!! Thank you very much. :) Cheers!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1