Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Blatant Assignment Help

Name: Anonymous 2009-04-03 3:40

hey /prog/

I'm having some trouble with my python assignment and was wondering if you could offer some help.

The bit i am having trouble with is i have to read a bunch of names from a text file that have values after them and sort them out.

the file looks like this

steve A6 A3 A1
kevin A2 A6
nikki A1 A5 A4

what I'm trying to is sort them into nested lists (dictionaries?) or a better way if you know of one.

eg. list(0) = steve
     steve(1) = A3
     list(2(2) = A5
      etc.

but I have no idea how to go about it, I've got functions to read the files char by char or by line, and then split them at '\n' but I don't know the best way to actually sort them.

thanks for your help.

Name: Anonymous 2009-04-03 3:49

perl -ane'$x{@F[0]} = [@F[1..$#F]]'

Name: Anonymous 2009-04-03 3:55

Dunno about Python because I'm not a fag, but I bet this will be helpful.
http://wiki.python.org/moin/HowTo/Sorting

Your nested lists thing will work, but it's kind of dumb to use a list for data of known format. I'm not really clear on what the values after the names are supposed to be. If it's actually a list of values, carry on. If it's some data with a known format, e.g. ((STEVE 555-4541) (MIKE 555-6790)), then a struct or a class or somesuch is generally more appropriate, since it gives a name to each of the values stored in it. Otherwise you end up writing a bunch of accessors like (defun contact-name (contact) (first contact)) (defun contact-number (contact) (second contact)), just to make your code readable and maintainable.

Now, I don't know what facilities Python provides in this regard, so think about it for yourself.

Name: Anonymous 2009-04-03 3:56

>>2
uhhh thanks? but I actually needed this for Python.

Name: Anonymous 2009-04-03 3:58

>>4
doh ho ho
haven't been here long, eh?

Name: Anonymous 2009-04-03 3:59

>>3
ha ha ha, yeah it's shitting me too. But it's the language the lecturer picked.

Basically what the values are, are names, followed by classes that can be attended and in what order they'd prefer them.

Name: Anonymous 2009-04-03 4:02

>>5

nope

Name: Anonymous 2009-04-03 4:28

Here you go OP. I was going to make an Attribute class and have that implement its own comparable interface, upon which each name has a set of its ordered attributes, and names can be compared based on their attributes but I decided I can't be fucked.

import java.util.*;
import java.io.*;
import java.util.regex.*;
public class Names implements Comparable<Names> {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("inputfile.txt"));
        String text = "", line = "";
        while((line=br.readLine())!=null)
            text+=line+"\n";
        String[] lines = text.split("\r?\n");
        ArrayList<Names> list = new ArrayList<Names>(lines.length);
        Pattern p = Pattern.compile("([a-zA-Z]+)\\s+(([A-Z]\\d\\s*)+)\\s*",Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
        Matcher m;
        for(int i=0; i<lines.length; i++) {
            m = p.matcher(lines[i]);
            if(m.find()) {
                list.add(new Names(m.group(1), m.group(2).split("\\s+")));
            }
        }
        Collections.sort(list);
        for(Names name : list) {
            System.out.print(name+" ");
            String[] attrs = name.getAttributes();
            for(String attr : attrs) {
                System.out.print(attr+" ");
            }
            System.out.println();
        }
    }
    private String name;
    private String[] attributes;
    public Names(String name, String... attributes) {
        this.name = name;
        this.attributes = attributes;
    }
    public int compareTo(Names o) {
        return this.name.compareTo(((Names)o).getName()); //could compare attributes here if wished
    }
    public String getName() {
        return this.name;
    }
    public String toString() {
        return this.name;
    }
    public String[] getAttributes() {
        return this.attributes;
    }
}

Name: Anonymous 2009-04-03 4:29

>>8
| Pattern.MULTILINE);
You may want to remove that, it is a leftover artifact from earlier.

Name: Anonymous 2009-04-03 4:34

>>5
>>6
>>7
this is the sort of thing that can only pahhend on /hreog/

Name: Anonymous 2009-04-03 4:41

>>8

oh god, i'm glad I gave up trying to learn whatever variant of c/c++/c# that is.

Name: Anonymous 2009-04-03 4:48

>>1
inputFile = file('ihbt.txt')
names = dict()
for line in inputFile:
    line = line.split()
    name, numbers = line[0], line[1:]
    numbers.sort()
    names[name] = numbers

Name: Anonymous 2009-04-03 4:51

>>11
java.java.java
variant of c/c++/c#

Name: Anonymous 2009-04-03 4:56

>>13
YHBT
HIBMT?

Name: Anonymous 2009-04-03 4:56

>>1
-module(homework).
-export([make_dict/0]).

make_dict() -> make_dict(dict:new()).
make_dict(D) ->
    case io:get_line("") of
        Line when list(Line) ->
            [K|V] = string:tokens(Line, " \n"),
            make_dict(dict:store(K,V,D));
        _ -> D
    end.

Name: Anonymous 2009-04-03 5:10

Kill yourself. Or at least stop programming.

Name: Anonymous 2009-04-03 5:13

>>14
They need to do way instain mother.

Name: Anonymous 2009-04-03 6:50

>>16

I would gladly do that, stop programming, unfortunately it's one of my required classes.

Name: Anonymous 2009-04-03 9:12

>>18
You do not belong here. GET OUT.

Name: Anonymous 2010-12-25 18:44

Name: Anonymous 2011-01-31 20:50

<-- check em dubz

Don't change these.
Name: Email:
Entire Thread Thread List