Mercurial log/diff for range of lines in a file - mercurial

I'd like an extension or tool that helps me browse the history of a range of lines in a given file. Say I want to look at the history of a particular function, currently at lines [start, end]. hg annotate gets me started:
AAA 772 06-Aug-02: void Graphics2DDXF::lineTo(double x, // the x coordinate
AAA 772 06-Aug-02: double y // the y cooordinate
AAA 772 06-Aug-02: )
AAA 772 06-Aug-02: {
BBB 2034 30-Aug-04: LOG;
BBB 6989 05-Dec-11:
BBB 4638 31-Oct-07: transform_->transform(&x,&y);
AAA 772 06-Aug-02:
BBB 7011 06-Jan-12: AGcRoot<Line> line = gcnew Line;
BBB 6989 05-Dec-11:
BBB 6989 05-Dec-11: Point3d startPoint(lastPenLocation_->x(), lastPenLocation_->y(), 0.0);
BBB 6989 05-Dec-11: Point3d endPoint(x, y, 0.0);
BBB 6989 05-Dec-11: line->StartPoint = startPoint;
BBB 6989 05-Dec-11: line->EndPoint = endPoint;
BBB 6989 05-Dec-11:
BBB 4638 31-Oct-07: lastPenLocation_ = APoint2D::New(x,y,AToleranceID::None);
BBB 7011 06-Jan-12:
BBB 7011 06-Jan-12: setAndAddEntity(line);
AAA 772 06-Aug-02: }
The last change to this method was changeset 7011. I can examine that with 'hg diff -c7011'.
The hard part is what happened before that. Starting with the annotate output for 7011-1:
% hg annotate -r7010 file.cpp
...
AAA 772 06-Aug-02: void Graphics2DDXF::lineTo(double x, // the x coordinate
AAA 772 06-Aug-02: double y // the y cooordinate
AAA 772 06-Aug-02: )
AAA 772 06-Aug-02: {
BBB 2034 30-Aug-04: LOG;
BBB 6989 05-Dec-11:
BBB 4638 31-Oct-07: transform_->transform(&x,&y);
AAA 772 06-Aug-02:
BBB 6989 05-Dec-11: Line^ line = gcnew Line;
AAA 772 06-Aug-02: addEntityToModelSpace(line);
AAA 772 06-Aug-02:
AAA 772 06-Aug-02: ensureLayerAvailable();
BBB 6989 05-Dec-11: line->LayerId = s_currentLayerObjectId;
BBB 6989 05-Dec-11:
BBB 6989 05-Dec-11: Point3d startPoint(lastPenLocation_->x(), lastPenLocation_->y(), 0.0);
BBB 6989 05-Dec-11: Point3d endPoint(x, y, 0.0);
BBB 6989 05-Dec-11: line->StartPoint = startPoint;
BBB 6989 05-Dec-11: line->EndPoint = endPoint;
BBB 6989 05-Dec-11:
BBB 6989 05-Dec-11: line->LinetypeId = currentLinetypeId();
BBB 6989 05-Dec-11: line->ColorIndex = dwgColor(getColor());
BBB 4638 31-Oct-07: lastPenLocation_ = APoint2D::New(x,y,AToleranceID::None);
AAA 772 06-Aug-02: }
So now I can see that the previous changeset affecting this range of lines was 6989. And so on.
It would be great to have a visual tool that did this, but I would be happy with something that just gave me the sequence of changesets: 7011, 6989, etc.
It wouldn't be hard to filter the annotate output for the linenumber range and find the maximum changeset number. What is hard is adjusting the range of lines to account for lines added and removed, especially when 'diff' claims that a change spanned the min or max of your range of lines. At least that was hard with CVS diff output, I haven't tried it with hg diff's output.
If the tool/extension I'm dreaming of doesn't exist, are there at least any tools for computing the modified line numbers?
Thanks,

It's not built for it, but you can approximate that by using hg grep to search for . which means anything in regexp. That'll get you output like this:
ry4an#four:~/projects/unblog$ hg grep --all --user --date . config.yaml
config.yaml:79:-:ry4an-hg:Mon Apr 23 22:45:24 2012 -0400:TAGLINE: I think in a monospaced font.
config.yaml:79:+:ry4an-hg:Mon Apr 23 22:45:24 2012 -0400:TAGLINE: A blog about software, projects, and other little things I build.
config.yaml:52:-:ry4an-hg:Sat Apr 30 11:25:08 2011 -0500:AUTHOR: Ry4an Brase
config.yaml:52:-:ry4an-hg:Sat Apr 30 11:25:08 2011 -0500:#DISQUS: rafaelmartins-en
config.yaml:52:-:ry4an-hg:Sat Apr 30 11:25:08 2011 -0500:TIMEZONE: UTC
config.yaml:52:+:ry4an-hg:Sat Apr 30 11:25:08 2011 -0500:#POSTS_PER_PAGE: 10
config.yaml:52:+:ry4an-hg:Sat Apr 30 11:25:08 2011 -0500:AUTHOR: Ry4an Brase
config.yaml:52:+:ry4an-hg:Sat Apr 30 11:25:08 2011 -0500:#DISQUS: your-disqus-id
config.yaml:52:+:ry4an-hg:Sat Apr 30 11:25:08 2011 -0500:TIMEZONE: UTC
config.yaml:0:+:ry4an-hg:Sat Jan 22 22:51:22 2011 -0600:TITLE: Ry4an's Unblog
config.yaml:0:+:ry4an-hg:Sat Jan 22 22:51:22 2011 -0600:TITLE_HTML: Ry4an's Unblog
config.yaml:0:+:ry4an-hg:Sat Jan 22 22:51:22 2011 -0600:TAGLINE: I think in a monospaced font.
config.yaml:0:+:ry4an-hg:Sat Jan 22 22:51:22 2011 -0600:AUTHOR: Ry4an Brase
config.yaml:0:+:ry4an-hg:Sat Jan 22 22:51:22 2011 -0600:#DISQUS: rafaelmartins-en
config.yaml:0:+:ry4an-hg:Sat Jan 22 22:51:22 2011 -0600:TIMEZONE: UTC
Where + means a line was added and - means it was removed. The --all option means show all matches, not just the most recent match per line.

Related

Add values in a lists which is a string column in hive

I have a set of data where a columns consists of lists which is of string data type.
Column_A|Column_B
AAA |1 23 56 89 74 52
BBB |63 99 44 2 80 87 58 63
CCC |96 45 23 84 62 74
Here, In the above data I need to add the values in column B as below:
Column_A|Column_B |Column_C
AAA |1 23 56 89 74 52 |295
BBB |63 99 44 2 80 87 58 63|496
CCC |96 45 23 84 62 74 |384
I have used cast function and converted the data type from string to int using the below query.
select Column_A,cast (Column_B as INT) as Column_B from Xyz
But summing the values is a great challenge.
Can someone help me out?
I'm learning RegEx too.. Is there any possibility to use RegEx?
Explode your column using split by space and aggregate.
This is demo in Hive:
with your_data as
(
select Column_A,Column_B from
(
select stack(3,
'AAA','1 23 56 89 74 52',
'BBB','63 99 44 2 80 87 58 63',
'CCC','96 45 23 84 62 74'
) as (Column_A,Column_B)
)s
) --Use your table instead of this CTE
select Column_A,Column_B, sum(cast(b.val_b as int)) as Column_C
from your_data a
lateral view outer explode(split(Column_B,' ')) b as val_b
group by Column_A,Column_B;
Result:
OK
AAA 1 23 56 89 74 52 295
BBB 63 99 44 2 80 87 58 63 496
CCC 96 45 23 84 62 74 384
Time taken: 53.228 seconds, Fetched: 3 row(s)
Alternatively, if the maximum number of elements in the list is fixed, you can do the same without explode, it will work much faster:
create temporary macro cast_value(s string) nvl(cast(s as int),0);
with your_data as
(
select Column_A,Column_B from
(
select stack(3,
'AAA','1 23 56 89 74 52',
'BBB','63 99 44 2 80 87 58 63',
'CCC','96 45 23 84 62 74'
) as (Column_A,Column_B)
)s
) --Use your table instead of this CTE
select Column_A,Column_B,
cast_value(col_B_array[0])+
cast_value(col_B_array[1])+
cast_value(col_B_array[2])+
cast_value(col_B_array[3])+
cast_value(col_B_array[4])+
cast_value(col_B_array[5])+
cast_value(col_B_array[6])+
cast_value(col_B_array[7])+
cast_value(col_B_array[8])+
cast_value(col_B_array[9]) as Column_C
from(
select Column_A,Column_B, split(Column_B,' ') col_B_array
from your_data a
)s
Result:
OK
AAA 1 23 56 89 74 52 295
BBB 63 99 44 2 80 87 58 63 496
CCC 96 45 23 84 62 74 384
Time taken: 0.82 seconds, Fetched: 3 row(s)

Access 2013 - Sort Irregular lengths strings to oder

I'm trying to sort some strings with hexa numbers, my problem is that they are to irregular and hard for my knowledge in Access so I could really use some help!
From every file "Files" are one REQUEST string with a corresponding RESPONSE string they are similar at the first 4 characters "16xx" and always at the 8-9 character "xx" sometimes in more places and at character 5-6 are +40 added to the RESPONSE ex 19 -> 59. I took some examples from my table (the real table is 600 rows with different string from 24 different files)
ID = pimekey, Files = file where string came form, Nr = what nr the string had in file, String = the string I would like to sort, TYPE = if it was a REQUEST or RESPONSE
I would like to make pairs of them in a new query like this...
...so that they are aliened in Files order with the REQUEST before the RESPONSE.
I have tried making different queries all day to sort this out but can´t get the syntax right. Tried sorting through using SQL left, iif , mid,len function with Update queries, but I either get syntax error, nothing or the wrong values... Is there a way of doing this or are they to irregular to even sort?
Thanks
EDIT
from one file how it look now:
ID Files Nr String Type
1 1 1 1636 19 02 2F REQUEST
2 2 2 1637 19 02 2F REQUEST
3 2 3 1631 19 02 2F REQUEST
4 3 4 1637 19 04 0A 1B 47 FF REQUEST
28 1 10 1636 59 02 FF RESPONSE
29 2 11 1637 59 02 FF RESPONSE
30 2 12 1631 59 02 7F C1 00 00 28 C2 A4 RESPONSE
31 3 13 1637 59 04 0A 1B 47 00 RESPONSE
how I would want it:
ID Files Nr String Type
1 1 1 1636 19 02 2F REQUEST
28 1 10 1636 59 02 FF RESPONSE
2 2 2 1637 19 02 2F REQUEST
29 2 11 1637 59 02 FF RESPONSE
3 2 3 1631 19 02 2F REQUEST
30 2 12 1631 59 02 7F C1 00 00 28 C2 A4 RESPONSE
4 3 4 1637 19 04 0A 1B 47 FF REQUEST
31 3 13 1637 59 04 0A 1B 47 00 RESPONSE
You can try something like this (MYSQL). It use user defined variable to "generate" field for ordering. I suppose FIL is the name of the table:
SELECT ID, FILES, NR, STRING, TYPE
FROM (
SELECT *
, #o:= CASE WHEN TYPE='REQUEST' THEN #o+2 ELSE 0 END ord
, #p:= CASE WHEN TYPE= 'RESPONSE' THEN #p+2 ELSE 0 END ord2
, #o+#p AS ord_tot
FROM FIL A
CROSS JOIN (SELECT #o:=-1,#p:=2 ) T1
ORDER BY TYPE, FILES, NR
) B
ORDER BY ord_tot;
Output:
ID FILES NR STRING TYPE
1 1 1 1636 19 02 2F REQUEST
28 1 10 1636 59 02 FF RESPONSE
2 2 2 1637 19 02 2F REQUEST
29 2 11 1637 59 02 FF RESPONSE
3 2 3 1631 19 02 2F REQUEST
30 2 12 1631 59 02 7F C1 00 00 28 C2 A4 RESPONSE
4 3 4 1637 19 04 0A 1B 47 FF REQUEST
31 3 13 1637 59 04 0A 1B 47 00 RESPONSE
The simplest way to accomplish this would be to use MySQL's built hexadecimal format and then just use ORDER BY on the correct fields. This is of course assuming you can modify how your data is stored to conform to the hexadecimal format described.

finding max value from duplicate value in mysql

I am having a table structure like this:
Date Count name
2015-03-05 154903 AAA
2015-03-04 153591 AAA
2015-03-03 151277 AAA
2015-03-06 93997 BBB
2015-03-03 93294 BBB
2015-03-02 79006 BBB
2015-03-08 77324 BBB
2015-03-06 144588 AAA
2015-03-07 144119 AAA
2015-03-02 133543 AAA
2015-03-01 124435 AAA
2015-03-08 115227 AAA
2015-03-05 98866 BBB
2015-03-04 96673 BBB
2015-03-07 96298 BBB
2015-03-01 68724 BBB
2015-03-05 55748 CCC
2015-03-01 55322 CCC
I need to find the maximum count from every value
Eg.
AAA is having max value of 154903
BBB is having max value of 98866
CCC is having max value of 55322
how query in mysql to fetch the data like this.
Not much to it -- just use the max aggregate:
select name, max(countfield)
from yourtable
group by name
Like #GordonLinoff said, this is the very very basic of SQL.
A simple group by should do it...
SELECT name, max(Count)
FROM table
GROUP BY name
Please read some about the Group by clause.

Three digit PIDs in OBD II

I'm in the process of writing a java based application to interface with a car. For hardware I'm using one of the wifi ELM 327 modules available from Amazon (this one to be precise: http://www.amazon.com/gp/product/B00FQ7O88M/ref=oh_details_o00_s00_i00). To test that it's working, I picked up the DashCommand application for my iPhone.
To get a bit of a look into the protocol used to interface with the module, I recorded the network traffic of a session (via wireshark) where I went on a drive while the DashCommand app recorded it's usual metrics (the car used for testing was a 2009 Acura TSX).
Most of the traffic initially is what I would have expected from what I've read in the documentation of both the ELM 327 and OBD II. However, once I got going, DashCommand ended up using PIDs that I can't seem to figure out. An example of what I saw would be:
...
Request: 01 0B2
Response: 01 0B2
18 DA F1 11 03 41 0B 1B
18 DA F1 1D 03 41 0 B 1C
>
Request: 01 0C2
Response: 01 0C2
18 DA F1 1D 04 41 0C 0C A0
18 DA F1 11 04 4 1 0C 0C A0
>
Request: 01 0D2
Response: 01 0D2
18 DA F1 11 03 41 0D 00
18 DA F1 1D 03 41 0 D 00
>
Request: 01 101
Response: 01 0D2
01 101
18 DA F1 11 04 41 10 01 3E
>
Request: 01 332
Response: 01 332
18 DA F1 D1 03 41 33 62
18 DA F1 11 03 41 3 3 62
>
…
The full list of three digit PIDs I've found so far are: 031, 0B2, 0C2, 0D2, 101, 332, and 341. Each of these are sent with mode 01 as shown above.
Is there any documentation on these PIDs? Or am I misinterpreting what I'm seeing?
The last digit is not part of the PID.
The number after the PID represents the number of replies you're expecting. You can see that this is always a number.
For example, when you add a '1' to your normal 2-digit PID, the ELM327 will wait for exactly 1 response from an ECU. When 1 ECU has responded to the ECU, the ELM327 will directly respond that result to you.
When you add a '2', it will wait for 2 repsonses.
Source: ELM327 Documentation - Go to page 32.

Code-golf: Output multiplication table to the Console

Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
I recently pointed a student doing work experience to an article about dumping a multiplication table to the console. It used a nested for loop and multiplied the step value of each.
This looked like a .NET 2.0 approach. I was wondering, with the use of Linq and extension methods,for example, how many lines of code it would take to achieve the same result.
Is the stackoverflow community up to the challenge?
The challenge:
In a console application, write code to generate a table like this example:
01 02 03 04 05 06 07 08 09
02 04 06 08 10 12 14 16 18
03 06 09 12 15 18 21 24 27
04 08 12 16 20 24 28 32 36
05 10 15 20 25 30 35 40 45
06 12 18 24 30 36 42 48 54
07 14 21 28 35 42 49 56 63
08 16 24 32 40 48 56 64 72
09 18 27 36 45 54 63 72 81
As this turned into a language-agnostic code-golf battle, I'll go with the communities decision about which is the best solution for the accepted answer.
There's been alot of talk about the spec and the format that the table should be in, I purposefully added the 00 format but the double new-line was originally only there because I didn't know how to format the text when creating the post!
J - 8 chars - 24 chars for proper format
*/~1+i.9
Gives:
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81
This solution found by #earl:
'r(0)q( )3.'8!:2*/~1+i.9
Gives:
01 02 03 04 05 06 07 08 09
02 04 06 08 10 12 14 16 18
03 06 09 12 15 18 21 24 27
04 08 12 16 20 24 28 32 36
05 10 15 20 25 30 35 40 45
06 12 18 24 30 36 42 48 54
07 14 21 28 35 42 49 56 63
08 16 24 32 40 48 56 64 72
09 18 27 36 45 54 63 72 81
MATLAB - 10 characters
a=1:9;a'*a
... or 33 characters for stricter output format
a=1:9;disp(num2str(a'*a,'%.2d '))
Brainf**k - 185 chars
>---------[++++++++++>---------[+<[-<+>>+++++++++[->+>>---------[>-<++++++++++<]<[>]>>+<<<<]>[-<+>]<---------<]<[->+<]>>>>++++[-<++++>]<[->++>+++>+++<<<]>>>[.[-]<]<]++++++++++.[-<->]<+]
cat - 252 characters
01 02 03 04 05 06 07 08 09
02 04 06 08 10 12 14 16 18
03 06 09 12 15 18 21 24 27
04 08 12 16 20 24 28 32 36
05 10 15 20 25 30 35 40 45
06 12 18 24 30 36 42 48 54
07 14 21 28 35 42 49 56 63
08 16 24 32 40 48 56 64 72
09 18 27 36 45 54 63 72 81
Assuming that a trailing newline is wanted; otherwise, 251 chars.
* runs *
Python - 61 chars
r=range(1,10)
for y in r:print"%02d "*9%tuple(y*x for x in r)
C#
This is only 2 lines. It uses lambdas not extension methods
var nums = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
nums.ForEach(n => { nums.ForEach(n2 => Console.Write((n * n2).ToString("00 "))); Console.WriteLine(); });
and of course it could be done in one long unreadable line
new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 }.ForEach(n => { new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 }.ForEach(n2 => Console.Write((n * n2).ToString("00 "))); Console.WriteLine(); });
all of this is assuming you consider a labmda one line?
K - 12 characters
Let's take the rosetta-stoning seriously, and compare Kdb+'s K4 with the canonical J solution (*/~1+i.9):
a*/:\:a:1+!9
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81
J's "table" operator (/) equals the K "each-left each-right" (/:\:) idiom. We don't have J's extremely handy "reflexive" operator (~) in K, so we have to pass a as both left and right argument.
Fortran95 - 40 chars (beating perl by 4 chars!)
This solution does print the leading zeros as per the spec.
print"(9(i3.2))",((i*j,i=1,9),j=1,9);end
Oracle SQL, 103 characters:
select n, n*2, n*3, n*4, n*5, n*6, n*7, n*8, n*9 from (select rownum n from dual CONNECT BY LEVEL < 10)
C# - 117, 113, 99, 96, 95 89 characters
updated based on NickLarsen's idea
for(int x=0,y;++x<10;)
for(y=x;y<x*10;y+=x)
Console.Write(y.ToString(y<x*9?"00 ":"00 \n"));
99, 85, 82 81 characters
... If you don't care about the leading zeros and would allow tabs for alignment.
for(int x=0,y;++x<10;)
{
var w="";
for(y=1;++y<10;)
w+=x*y+" ";
Console.WriteLine(w);
}
COBOL - 218 chars -> 216 chars
PROGRAM-ID.P.DATA DIVISION.WORKING-STORAGE SECTION.
1 I PIC 9.
1 N PIC 99.
PROCEDURE DIVISION.PERFORM 9 TIMES
ADD 1 TO I
SET N TO I
PERFORM 9 TIMES
DISPLAY N' 'NO ADVANCING
ADD I TO N
END-PERFORM
DISPLAY''
END-PERFORM.
Edit
216 chars (probably a different compiler)
PROGRAM-ID.P.DATA DIVISION.WORKING-STORAGE SECTION.
1 I PIC 9.
1 N PIC 99.
PROCEDURE DIVISION.
PERFORM B 9 TIMES
STOP RUN.
B.
ADD 1 TO I
set N to I
PERFORM C 9 TIMES
DISPLAY''.
C.
DISPLAY N" "NO ADVANCING
Add I TO N.
Not really a one-liner, but the shortest linq i can think of:
var r = Enumerable.Range(1, 9);
foreach (var z in r.Select(n => r.Select(m => n * m)).Select(a => a.Select(b => b.ToString("00 "))))
{
foreach (var q in z)
Console.Write(q);
Console.WriteLine();
}
In response to combining this and SRuly's answer
Enumberable.Range(1,9).ToList.ForEach(n => Enumberable.Range(1,9).ToList.ForEach(n2 => Console.Write((n * n2).ToString("00 "))); Console.WriteLine(); });
Ruby - 42 Chars (including one linebreak, interactive command line only)
This method is two lines of input and only works in irb (because irb gives us _), but shortens the previous method by a scant 2 charcters.
1..9
_.map{|y|puts"%02d "*9%_.map{|x|x*y}}
Ruby - 44 Chars (tied with perl)
(a=1..9).map{|y|puts"%02d "*9%a.map{|x|x*y}}
Ruby - 46 Chars
9.times{|y|puts"%02d "*9%(1..9).map{|x|x*y+x}}
Ruby - 47 Chars
And back to a double loop
(1..9).map{|y|puts"%02d "*9%(1..9).map{|x|x*y}}
Ruby - 54 chars!
Using a single loop saves a couple of chars!
(9..89).map{|n|print"%02d "%(n/9*(x=n%9+1))+"\n"*(x/9)}
Ruby - 56 chars
9.times{|x|puts (1..9).map{|y|"%.2d"%(y+x*y)}.join(" ")}
Haskell — 85 84 79 chars
r=[1..9]
s x=['0'|x<=9]++show x
main=mapM putStrLn[unwords[s$x*y|x<-r]|y<-r]
If double spacing is required (89 81 chars),
r=[1..9]
s x=['0'|x<=9]++show x
main=mapM putStrLn['\n':unwords[s$x*y|x<-r]|y<-r]
F# - 61 chars:
for y=1 to 9 do(for x=1 to 9 do printf"%02d "(x*y));printfn""
If you prefer a more applicative/LINQ-y solution, then in 72 chars:
[1..9]|>Seq.iter(fun y->[1..9]|>Seq.iter((*)y>>printf"%02d ");printfn"")
c# - 125, 123 chars (2 lines):
var r=Enumerable.Range(1,9).ToList();
r.ForEach(n=>{var s="";r.ForEach(m=>s+=(n*m).ToString("00 "));Console.WriteLine(s);});
C - 97 79 characters
#define f(i){int i=0;while(i++<9)
main()f(x)f(y)printf("%.2d ",x*y);puts("");}}
Perl, 44 chars
(No hope of coming anywhere near J, but languages with matrix ops are in a class of their own here...)
for$n(1..9){printf"%3d"x9 .$/,map$n*$_,1..9}
R (very similar to Matlab on this level): 12 characters.
> 1:9%*%t(1:9)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 1 2 3 4 5 6 7 8 9
[2,] 2 4 6 8 10 12 14 16 18
[3,] 3 6 9 12 15 18 21 24 27
[4,] 4 8 12 16 20 24 28 32 36
[5,] 5 10 15 20 25 30 35 40 45
[6,] 6 12 18 24 30 36 42 48 54
[7,] 7 14 21 28 35 42 49 56 63
[8,] 8 16 24 32 40 48 56 64 72
[9,] 9 18 27 36 45 54 63 72 81
PHP, 71 chars
for($x=0;++$x<10;print"\n"){for($y=0;++$y<10;){printf("%02d ",$x*$y);}}
Output:
$ php -r 'for($x=0;++$x<10;print"\n"){for($y=0;++$y<10;){printf("%02d ",$x*$y);}}'
01 02 03 04 05 06 07 08 09
02 04 06 08 10 12 14 16 18
03 06 09 12 15 18 21 24 27
04 08 12 16 20 24 28 32 36
05 10 15 20 25 30 35 40 45
06 12 18 24 30 36 42 48 54
07 14 21 28 35 42 49 56 63
08 16 24 32 40 48 56 64 72
09 18 27 36 45 54 63 72 81
C#, 135 chars, nice and clean:
var rg = Enumerable.Range(1, 9);
foreach (var rc in from r in rg
from c in rg
select (r * c).ToString("D2") + (c == 9 ? "\n\n" : " "))
Console.Write(rc);
PostgreSQL: 81 74 chars
select array(select generate_series(1,9)*x)from generate_series(1,9)as x;
Ruby - 56 chars :D
9.times{|a|9.times{|b|print"%02d "%((a+1)*(b+1))};puts;}
C - 66 Chars
This resolves the complaint about the second parameter of main :)
main(x){for(x=8;x++<89;)printf("%.2d%c",x/9*(x%9+1),x%9<8?32:10);}
C - 77 chars
Based on dreamlax's 97 char answer. His current answer somewhat resembles this one now :)
Compiles ok with gcc, and main(x,y) is fair game for golf i reckon
#define f(i){for(i=0;i++<9;)
main(x,y)f(x)f(y)printf("%.2d ",x*y);puts("");}}
XQuery 1.0 (96 bytes)
string-join(for$x in 1 to 9 return(for$y in 1 to 9 return concat(0[$x*$y<10],$x*$y,' '),'
'),'')
Run (with XQSharp) with:
xquery table.xq !method=text
Scala - 77 59 58 chars
print(1 to 9 map(p=>1 to 9 map(q=>"%02d "format(p*q))mkString)mkString("\n"))
Sorry, I had to do this, the Scala solution by Malax was way too readable...
[Edit] For comprehension seems to be the better choice:
for(p<-1 to 9;q<-{println;1 to 9})print("%02d "format p*q)
[Edit] A much longer solution, but without multiplication, and much more obfuscated:
val s=(1 to 9).toSeq
(s:\s){(p,q)=>println(q.map("%02d "format _)mkString)
q zip(s)map(t=>t._1+t._2)}
PHP, 62 chars
for(;$x++<9;print"\n",$y=0)while($y++<9)printf("%02d ",$x*$y);
Java - 155 137 chars
Update 1: replaced string building by direct printing. Saved 18 chars.
class M{public static void main(String[]a){for(int x,y=0,z=10;++y<z;System.out.println())for(x=0;++x<z;System.out.printf("%02d ",x*y));}}
More readable format:
class M{
public static void main(String[]a){
for(int x,y=0,z=10;++y<z;System.out.println())
for(x=0;++x<z;System.out.printf("%02d ",x*y));
}
}
Another attempt using C#/Linq with GroupJoin:
Console.Write(
String.Join(
Environment.NewLine,
Enumerable.Range(1, 9)
.GroupJoin(Enumerable.Range(1, 9), y => 0, x => 0, (y, xx) => String.Join(" ", xx.Select(x => x * y)))
.ToArray()));
Ruby — 47 chars
puts (a=1..9).map{|i|a.map{|j|"%2d"%(j*i)}*" "}
Output
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81
(If we ignore spacing, it becomes 39: puts (a=1..9).map{|i|a.map{|j|j*i}*" "} And anyway, I feel like there's a bit of room for improvement with the wordy map stuff.)