break line after some number of measures in lilypond - line

I have this as a sample notation example. How would I specify to have
exactly 4 measures per line, throughout the whole piece. I searched the whole
documentation and did not find such a simple feature. Am I missing something?
\version "2.18.2"
% comment line
\header {
title = "Ruska narodna pesma."
composer = "Narodna pesma"
subtitle = "-za gitaru-"
}
melody = {
\key c \major
\time 3/4
e' f e |
e, gis b |
d c b |
a c e |
}
\score {
\relative c' <<
\new Staff \melody
>>
\layout {
}
}

Ok, I have found the answer on mailing lists.. so here it is as a future reference for those who might have it in need.. *(thanks David)
\version "2.18.2"
% comment line
\header {
title = "Ruska narodna pesma."
composer = "Narodna pesma"
subtitle = "-za gitaru-"
}
melody = {
\key c \major
\time 3/4
e' f e |
e, gis b |
d c b |
% **EDIT** use manual breaks where you want them to appear
a c e | \break
}
\score {
\relative c' <<
\new Staff \melody
>>
\layout {
% **EDIT** also here, specify ragged-right property
ragged-right = ##f
}
}

You should have found the answer first on the documentation. If you go to the index of the Notation Reference and search for "line break" you'll get to the relevant chapter.
And if you scroll down you can see another way of adding line breaks, i.e. using a voice containing spacing rests (so invisible) and place the line breaks there. This strategy, further explained in this section, has the advantage of letting you toggling on/off the manual line break.

Related

In LiliyPond, how do you alter a music expression?

I have a situation where I have a recurring piece of music with only slight variations. I want to lay down a base piece as a variable. Then reuse it multiple times, each time altering it by replacing a few notes or measures. Here's a simplified example
base = { c4 c c c }
% pseudo function \replace MUSIC FROM TO ORIG
% FROM and To are pairs of numbers COUNT DURATION
from = #'(1 2)
to = #'(3 4)
var1 = \replace { d4 } \from \to \base
% this means replace
% from "after 1 half note into the music"
% to "after 3 quarter notes into the music"
% that is replace the third beat with { d4 }
% this will be equivalent to
var1 = { c4 c d c }
How do I do this?
A LilyPond solution to this is \tag. I haven't found a built-in functionality for altering a music expression on the fly. However, for the variations usecase, tags serve this purpose. The above example would be this
base = { c4 c \tag #'base c \tag #'var1 d c }
var1 = \keepWithTag #'var1 \base

Find all numbers adding up to given number and range to use

This is not homework, but a real world application I'm getting stumped on.
The best way I can explain my problem is as follows:
Imagine you have 3 pig pens A, B, and C and 10 pigs to be kept.
The pens have restrictions in that each pen must contain at least 1 pig and A.pigs <= B.pigs <= C.pigs. List all the possible pen configurations.
The REAL application can have anywhere between 1 and 7 pens, and anywhere between numPens and numPens*30 pigs and no pen may contain more than 30 pigs. So essentially this translates into "what numbers between 1 and 30 (repeats allowed) add up to X using Y numbers"
This could be solved simply with nested loops, but thats a terrible inefficient solution especially knowing the scope of the real application:
var cnt = 0
val target = 10
for (a in 1..30) {
for (b in a..30) {
val c = target - a - b
cnt++
if (a <= b && b <= c && a + b + c == target) {
println("$a $b $c")
}
}
}
println(cnt)
output:
1 1 8
1 2 7
1 3 6
1 4 5
2 2 6
2 3 5
2 4 4
3 3 4
465
I'm sure there is a recursive solution to this problem. But I'm having trouble even finding the starting point for this one.
It seems easy to start with an array of [1, 1, 8] where each index represents a pen. Just put all the pigs in 1 pen and move them around 1 at a time while following the constraints.
Subtract from C, add to B as much as you can while keeping constraints gets us [1, 2, 7], [1, 3, 6], [1, 4, 5] but at that point I'm stuck code wise.
What I have currently:
fun main(vararg args: String) {
val list = arrayOf(1, 1, 8)
list.forEach { print("$it\t") }
println()
rec(list, list.lastIndex - 1)
}
fun rec(list: Array<Int>, index: Int) {
if (index == list.lastIndex || index < 0) {
return
}
while (list[index] + 1 <= list[index + 1] - 1) {
list[index]++
list[index + 1]--
list.forEach { print("$it\t") }
println()
}
}
Obviously I need to call rec somewhere within itself and probably need some conditionals to call rec correctly (possibly adding more parameters to it). The question is where and how? Normally not bad with recursion, but this one is stumping me.
Solution:
I got so hung up on the idea I needed recursion. This loop seems to work pretty well.
for (a in 1..30) {
// we know a and c must be at least b, so cap range
var b = a
while(b < min(target - a - b, 30)){
val c = target - a - b
cnt++
if (a <= b && b <= c && a + b + c == target) {
println("$a $b $c")
}
b++
}
}
Some inefficiencies to note:
Once you know a and b, the only value for c that could make a solution would be 10-a-b, so you don't need the innermost loop at all.
Since a<=b, the second loop should start at a, not 1.
You can also limit the top of the second loop's range based on the value of a

AWK: Parsing a directed graph / Extracting records from a file based on fields matching parts of an input file

Problem
So I have an input file with three fields. It is basically a list describing a type of directed graph. The first field is the starting node, second connection type (as in this case there is more than one), and last is the node projected onto.
The problem is, this is a very large and unruly direct graph, and I am only interested in some paths. So I want to provide an input file which has a name of nodes that I care about. If the nodes are mentioned in either the first or third field of the graph file, then I want that entire record (as the path type might vary).
Question
How to extract only certain records of a directed graph?
Bonus, how to extract only those paths which join nodes of interest by at most one neighbor (i.e. nodes of interest can be second nearest neighbors).
Request
I am trying to improve my AWK programming, which is why 1) I want to do this in AWK and 2) I would greatly appreciate a verbose explanation of the code :)
Example of the Problem
Input file:
A
C
D
File to parse:
A -> B
A -> C
A -> D
B -> A
B -> D
C -> E
D -> F
E -> B
E -> F
F -> C
...
Output:
A -> B
A -> C
A -> D
B -> A
B -> D
C -> E
D -> F
F -> C
Bonus Example:
A -> B -> D -> F -> C
If I understand your problem correctly, then this will do:
awk 'NR==FNR { data[$1] = 1; next } $1 in data || $3 in data { print }' graph[12]
How it works: while reading the first file, add all interesting nodes to data. While reading the second file, print only the lines where field one or field three is in data, i.e. is an interesting node.
Going for the bonus:
function left(str) { # returns the leftmost char of a given edge (A -> B)
return substr(str,1,1)
}
function right(str) { # returns the rightmost...
return substr(str,length(str),1)
}
function cmp_str_ind(i1, v1, i2, v2) # array travese order function
{ # this forces the start from the node in the beginning of input file
if(left(i1)==left(a)&&left(i2)!=left(a)) # or leftmost value in a
return -1
else if(left(i2)==left(a)&&left(i1)!=left(a))
return 1
else if(i1 < i2)
return -1
return (i1 != i2)
}
function trav(a,b,c,d) { # goes thru edges in AWK order
# print "A:"a," C:"c," D:"d
if(index(d,c)||index(d,right(c))) {
return ""
}
d=d", "c # c holds the current edge being examined
if(index(a,right(c))) { # these edges affect a
# print "3:"c
sub(right(c),"",a)
if(a=="") { # when a is empty, path is found
print d # d has the traversed path
exit
}
for (i in b) {
if(left(i)==right(c)) # only try the ones that can be added to the end
trav(a,b,i,d)
}
a=a""right(c)
} else {
# print "4:"c
for (i in b)
if(left(i)==right(c))
trav(a,b,i,d)
}
}
BEGIN { # playing with the traverse order
PROCINFO["sorted_in"]="cmp_str_ind"
}
NR==FNR {
a=a""$0 # a has the input (ADC)
next
}
{
b[$0]=$0 # b has the edges
}
END { # after reading in the data, recursively test every path
for(i in b) # candidate pruning the unfit ones first. CLR or Dijkstra
if(index(a,left(i))) { # were not consulted on that logic.
# print "3: "i
sub(left(i),"",a)
trav(a,b,i,left(i))
a=a""left(i)
}
else {
# print "2: "i
trav(a,b,i,left(i))
}
}
$ awk -f graph.awk input parse
A, A -> D, D -> F, F -> C
If you uncomment the BEGIN part, you get the A, A -> B, B -> D, D -> F, F -> C. I know, I should work on it more and comment it better but it's midnight over here. Maybe tomorrow.

Graphivz does not render edge label when it's connecting two nodes with shape=point

I have the following graph description:
graph G {
{rank=same a b}
a[shape=point]
b[shape=point]
a -- b [label=e];
}
However, it outputs a single edge without a label (running graphviz with dot -Tpdf -o test.pdf test.dot):
Rendering to PNG yields same result. If I render it to PDF and then look for an "e" in the document, the following are is highlighted:
So, the edge label is here but it's invisible somewhy.
Surprisingly, if I switch rank direction, everything works:
graph G {
rankdir=LR;
a[shape=point]
b[shape=point]
a -- b [label=e];
}
Not an answer really as I cannot provide any explanation but what I have observed is: As long as there is any other character / label rendered in the graph,
the e will also be rendered.
Interestingly enough this even applies for labels that are explicitly set to invisible, so if you compile
graph G
{
{ rank=same; a; b; }
a [ shape = point ];
b [ shape = point ];
a -- b [ label = "e" ];
\\ add this
c [ style = invis ];
}
you get what you want:
The graph area will increase, though.

Is there a less ugly way to do input in D than scanf()?

Currently the only way I know how to do input in D is with the scanf() function. But god damn it's ugly. You would think that since it's an upgrade from C that they would have fixed that.
I'm looking for a way to do it with a single argument. Currently you have to do:
int foo = 0;
scanf("%i", &foo);
writeln("%i", foo);
But it would look a lot cleaner with a single argument. Something like:
int foo = 0;
scanf(foo);
writeln(foo);
Thanks.
readf("%d", &foo); allows working with std.stdio.File rather than C FILE*
foo = readln().strip().to!int();
For reading entire files with lines formatted in the same way:
int[] numbers = slurp!int("filename", "%d");
There's a really cool user-input module here:
https://github.com/Abscissa/scriptlike/blob/master/src/scriptlike/interact.d
Example code:
if (userInput!bool("Do you want to continue?"))
{
auto outputFolder = pathLocation("Where you do want to place the output?");
auto color = menu!string("What color would you like to use?", ["Blue", "Green"]);
}
auto num = require!(int, "a > 0 && a <= 10")("Enter a number from 1 to 10");
The above answers are great. I just want to add my 2 cents.
I often have the following simple function lying around:
T read(T)()
{
T obj;
readf(" %s", &obj);
return obj;
}
It's generic and pretty handy - it swallows any white space and reads any type you ask. You can use it like this:
auto number = read!int;
auto floating_number = read!float;
// etc.