Author Topic: LIKE +  (Read 19290 times)

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: LIKE +
« Reply #45 on: November 13, 2018, 04:34:42 PM »
Is there anyone besides me willing to post a working EXTRACT / MATCH submission for this code challenge?

I think they got tired of waiting for yours... ;D ;D ;D ;D

Seriously, nice work!!

AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 3562
    • ScriptBasic Open Source Project
Re: LIKE +
« Reply #46 on: November 13, 2018, 06:02:54 PM »
I had the concept from the start. The sandbox project took more of my time then I anticipated.

Thanks! I think it turned out nice as well.

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: LIKE +
« Reply #47 on: November 13, 2018, 06:16:48 PM »
Here's a PYTHONIC take on your approach:

Code: Python
  1. #!/usr/bin/env python
  2.  
  3. html = """<!DOCTYPE html>
  4. <html>
  5.  <head>
  6.    <title>AllBASIC.INFO Forum LIKE Code Challenge</title>
  7.  </head>
  8.  <body>
  9. LIKE it or don't.
  10.  </body>
  11. </html>"""
  12.  
  13. MATCH={}
  14. def extract( source, filter):
  15.     MATCH.clear()
  16.     try:
  17.         d = filter.split('*')
  18.         for i, x in enumerate(d):
  19.             if x and i % 2 != 0:
  20.                 start = source.index( d[i] ) + len( d[i] )
  21.                 end = source.index( d[i+1], start )
  22.                 if i == 1: MATCH[0]=source[start:end]
  23.                 MATCH[i]=( source[start:end].strip(), start, end-start )
  24.         return MATCH[0]
  25.     except:
  26.         return ""
  27.  
  28. print extract(html,"*<title>*</title>*<body>*</body>*"),"\n"
  29.  
  30. print "Title: ",MATCH[1][0]
  31. print "Start: ",MATCH[1][1]
  32. print "Length: ",MATCH[1][2],"\n"
  33.  
  34. print "Body: ",MATCH[3][0]
  35. print "Start: ",MATCH[3][1]
  36. print "Length: ",MATCH[3][2]
  37.  

[riveraa@MacDev ~/Projects/Python/Extract] $ ./extract.py
AllBASIC.INFO Forum LIKE Code Challenge

Title:  AllBASIC.INFO Forum LIKE Code Challenge
Start:  43
Length:  39

Body:  LIKE it or don't.
Start:  109
Length:  21


AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 3562
    • ScriptBasic Open Source Project
Re: LIKE +
« Reply #48 on: November 13, 2018, 06:33:34 PM »
That looks great!

Python is definatly my second choice in scripting languages. Perl comes in third.

Peter was a fan of Python and Perl. You can see traces of these languages in Scipt BASIC.

I think your start positions are off by one. Do Python strings start at zero?

« Last Edit: November 13, 2018, 11:26:02 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3562
    • ScriptBasic Open Source Project
Re: LIKE +
« Reply #49 on: November 14, 2018, 09:04:33 PM »
AIR,

This may or may not be of interest to you.

https://winpython.github.io/


Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: LIKE +
« Reply #50 on: November 14, 2018, 10:19:27 PM »
I think your start positions are off by one. Do Python strings start at zero?

Yes.

Offline John

  • Forum Support / SB Dev
  • Posts: 3562
    • ScriptBasic Open Source Project
Re: LIKE +
« Reply #51 on: November 14, 2018, 10:50:52 PM »
That sucks!

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: LIKE +
« Reply #52 on: November 15, 2018, 06:40:13 AM »
Zero-based indexing is pretty much the standard.

Offline John

  • Forum Support / SB Dev
  • Posts: 3562
    • ScriptBasic Open Source Project
Re: LIKE +
« Reply #53 on: November 15, 2018, 07:19:14 AM »
For arrays, not string starting position.

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: LIKE +
« Reply #54 on: November 15, 2018, 11:44:36 AM »
strings = char arrays = zero-based

Code: C
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5.  
  6. int main (int argc, char **argv) {
  7.  
  8.     char *string = "Hello, World!";
  9.  
  10.     printf("string = %s\n",string);
  11.     printf("string[0] = %c\n", string[0]);
  12.     printf("string[1] = %c\n", string[1]);
  13.    
  14.     return 0;
  15.  
  16. }

[arivera1271@arivera1271 ~/Projects/C] $ gcc string.c -o string
[arivera1271@arivera1271 ~/Projects/C] $ ./string
string = Hello, World!
string[0] = H
string[1] = e
[arivera1271@arivera1271 ~/Projects/C] $


AIR.

Edit: removed redundant strdup for this example...
« Last Edit: November 15, 2018, 11:53:01 AM by AIR »

Offline John

  • Forum Support / SB Dev
  • Posts: 3562
    • ScriptBasic Open Source Project
Re: LIKE +
« Reply #55 on: November 15, 2018, 12:39:41 PM »
I'm good with zero based character arrays emulating strings in various languages.

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: LIKE +
« Reply #56 on: November 15, 2018, 07:55:18 PM »
AIR,

This may or may not be of interest to you.

https://winpython.github.io/

Way too much bloat for my taste, but thanks!

AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 3562
    • ScriptBasic Open Source Project
Re: LIKE +
« Reply #57 on: November 15, 2018, 08:35:26 PM »
The only reason I thought they might have a chance with you is their efforts to containerized the environment. I didn't go far enough to see if there was a weight problem.

SB spoils me again only having < 800KB footprint. If I don't care about scriba's command line utility extensions, SB would have a 4KB footprint with the interpreter running out of a DLL (.so)

Where SB shines is running multi-threaded using a share object already resident in memory.
« Last Edit: November 15, 2018, 09:13:30 PM by John »