* evaluate cpp snippet in org babel with default includes and customized entry point
@ 2017-09-16 7:50 Amos Bird
2017-09-16 8:33 ` Thierry Banel
2017-09-16 8:46 ` Thierry Banel
0 siblings, 2 replies; 10+ messages in thread
From: Amos Bird @ 2017-09-16 7:50 UTC (permalink / raw)
To: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 731 bytes --]
#+OPTIONS: latex:t toc:nil H:3
Hi
I'd like to write a blog about leetcode solutions in c++. How can I evaluate those c++ code snippet using org babel?
* Array
** Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
#+BEGIN_SRC cpp
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
}
};
#+END_SRC
#+RESULTS:
I need to add default headers and pass arguments to the entry class.
--
Amos Bird
amosbird@gmail.com
[-- Attachment #2.1: Type: text/html, Size: 2212 bytes --]
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: evaluate cpp snippet in org babel with default includes and customized entry point
2017-09-16 7:50 evaluate cpp snippet in org babel with default includes and customized entry point Amos Bird
@ 2017-09-16 8:33 ` Thierry Banel
2017-09-16 8:37 ` Amos Bird
2017-09-16 8:46 ` Thierry Banel
1 sibling, 1 reply; 10+ messages in thread
From: Thierry Banel @ 2017-09-16 8:33 UTC (permalink / raw)
To: emacs-orgmode
A starting point could be as follows.
Type C-c C-c to evaluate.
Type C-c C-v v to expand the source.
#+BEGIN_SRC C++ :flags -std=c++11
#include <vector>
#include <iostream>
using namespace std;
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> result;
result.push_back(0);
result.push_back(1);
return result;
}
};
vector<int> nums = {2, 7, 11, 15};
int target = 9;
int main()
{
Solution solution;
for (int x : solution.twoSum(nums,target))
cout<<x<<"\n";
}
#+END_SRC
#+RESULTS:
| 0 |
| 1 |
On 16/09/2017 09:50, Amos Bird wrote:
>
> Hi
>
> I'd like to write a blog about leetcode solutions in c++. How can I
> evaluate those c++ code snippet using org babel?
>
>
> 1 Array
>
>
> 1.1 Two Sum
>
> Given an array of integers, return indices of the two numbers such
> that they add up to a specific target.
>
> You may assume that each input would have exactly one solution, and
> you may not use the same element twice.
>
> Example:
> Given nums = [2, 7, 11, 15], target = 9,
>
> Because nums[0] + nums[1] = 2 + 7 = 9,
> return [0, 1].
>
> |class Solution {
> public:
> vector<int> twoSum(vector<int>&nums,int target) {
>
> }
> };
> |
>
> I need to add default headers and pass arguments to the entry class.
>
> –
> Amos Bird
> amosbird@gmail.com
>
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: evaluate cpp snippet in org babel with default includes and customized entry point
2017-09-16 8:33 ` Thierry Banel
@ 2017-09-16 8:37 ` Amos Bird
2017-09-16 8:54 ` Thierry Banel
0 siblings, 1 reply; 10+ messages in thread
From: Amos Bird @ 2017-09-16 8:37 UTC (permalink / raw)
To: Thierry Banel; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 1691 bytes --]
#+OPTIONS: latex:t toc:nil H:3
Thanks Thierry,
But I'd like to hide all those includes and the main function. I'm not sure if it's possible.
Thierry Banel <tbanelwebmin@free.fr> writes:
> A starting point could be as follows.
> Type C-c C-c to evaluate.
> Type C-c C-v v to expand the source.
>
> #+BEGIN_SRC C++ :flags -std=c++11
>
> #include <vector>
> #include <iostream>
> using namespace std;
>
> class Solution {
> public:
> vector<int> twoSum(vector<int>& nums, int target) {
> vector<int> result;
> result.push_back(0);
> result.push_back(1);
> return result;
> }
> };
>
> vector<int> nums = {2, 7, 11, 15};
> int target = 9;
>
> int main()
> {
> Solution solution;
> for (int x : solution.twoSum(nums,target))
> cout<<x<<"\n";
> }
>
> #+END_SRC
>
> #+RESULTS:
> | 0 |
> | 1 |
>
>
>
> On 16/09/2017 09:50, Amos Bird wrote:
>>
>> Hi
>>
>> I'd like to write a blog about leetcode solutions in c++. How can I evaluate
>> those c++ code snippet using org babel?
>>
>>
>> 1 Array
>>
>>
>> 1.1 Two Sum
>>
>> Given an array of integers, return indices of the two numbers such that they
>> add up to a specific target.
>>
>> You may assume that each input would have exactly one solution, and you may
>> not use the same element twice.
>>
>> Example:
>> Given nums = [2, 7, 11, 15], target = 9,
>>
>> Because nums[0] + nums[1] = 2 + 7 = 9,
>> return [0, 1].
>>
>> |class Solution {
>> public:
>> vector<int> twoSum(vector<int>&nums,int target) {
>>
>> }
>> };
>> |
>>
>> I need to add default headers and pass arguments to the entry class.
>>
>> –
>> Amos Bird
>> amosbird@gmail.com
>>
--
Amos Bird
amosbird@gmail.com
[-- Attachment #2.1: Type: text/html, Size: 2665 bytes --]
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: evaluate cpp snippet in org babel with default includes and customized entry point
2017-09-16 8:37 ` Amos Bird
@ 2017-09-16 8:54 ` Thierry Banel
2017-09-16 8:58 ` Amos Bird
2017-09-16 9:05 ` Thierry Banel
0 siblings, 2 replies; 10+ messages in thread
From: Thierry Banel @ 2017-09-16 8:54 UTC (permalink / raw)
To: Amos Bird; +Cc: emacs-orgmode
You may use the :includes header parameter.
Also, you may omit the main() function. In this case, all your code will
become the body of a default main function. Type C-c C-v v on the
following example to understand what is happening.
#+BEGIN_SRC C++ :includes <iostream>
std::cout<<"hello";
#+END_SRC
#+RESULTS:
: hello
On 16/09/2017 10:37, Amos Bird wrote:
>
> Thanks Thierry,
>
> But I'd like to hide all those includes and the main function. I'm not
> sure if it's possible.
>
>
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: evaluate cpp snippet in org babel with default includes and customized entry point
2017-09-16 8:54 ` Thierry Banel
@ 2017-09-16 8:58 ` Amos Bird
2017-09-16 9:11 ` Thierry Banel
2017-09-16 9:05 ` Thierry Banel
1 sibling, 1 reply; 10+ messages in thread
From: Amos Bird @ 2017-09-16 8:58 UTC (permalink / raw)
To: Thierry Banel; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 914 bytes --]
#+OPTIONS: latex:t toc:nil H:3
Ok, i get the idea. So how can I customized the default code expansion so that it can directly evaluate this
#+BEGIN_SRC cpp
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
}
};
#+END_SRC
#+RESULTS:
Thierry Banel <tbanelwebmin@free.fr> writes:
> You may use the :includes header parameter.
>
> Also, you may omit the main() function. In this case, all your code will become
> the body of a default main function. Type C-c C-v v on the following example to
> understand what is happening.
>
>
> #+BEGIN_SRC C++ :includes <iostream>
> std::cout<<"hello";
> #+END_SRC
>
> #+RESULTS:
> : hello
>
>
> On 16/09/2017 10:37, Amos Bird wrote:
>>
>> Thanks Thierry,
>>
>> But I'd like to hide all those includes and the main function. I'm not sure if
>> it's possible.
>>
>>
--
Amos Bird
amosbird@gmail.com
[-- Attachment #2.1: Type: text/html, Size: 2188 bytes --]
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: evaluate cpp snippet in org babel with default includes and customized entry point
2017-09-16 8:58 ` Amos Bird
@ 2017-09-16 9:11 ` Thierry Banel
2017-09-16 9:14 ` Amos Bird
0 siblings, 1 reply; 10+ messages in thread
From: Thierry Banel @ 2017-09-16 9:11 UTC (permalink / raw)
To: emacs-orgmode@gnu.org >> Org Mode
[-- Attachment #1: Type: text/html, Size: 1829 bytes --]
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: evaluate cpp snippet in org babel with default includes and customized entry point
2017-09-16 9:11 ` Thierry Banel
@ 2017-09-16 9:14 ` Amos Bird
2017-09-16 9:36 ` Thierry Banel
0 siblings, 1 reply; 10+ messages in thread
From: Amos Bird @ 2017-09-16 9:14 UTC (permalink / raw)
To: Thierry Banel; +Cc: emacs-orgmode@gnu.org >> Org Mode
[-- Attachment #1: Type: text/plain, Size: 603 bytes --]
#+OPTIONS: latex:t toc:nil H:3
Hmm, is it possible to customize babel's c++ backend to achieve this? I may also want a evaluation that submits the code to online judge and returns the result.
Thierry Banel <tbanelwebmin@free.fr> writes:
> You still need to explicitly call twoSum(), and that cannot be hidden.
>
> On 16/09/2017 10:58, Amos Bird wrote:
>
> Ok, i get the idea. So how can I customized the default code expansion so that it can directly evaluate this
>
> class Solution {
> public:
> vector<int> twoSum(vector<int>& nums, int target) {
>
> }
> };
--
Amos Bird
amosbird@gmail.com
[-- Attachment #2.1: Type: text/html, Size: 773 bytes --]
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: evaluate cpp snippet in org babel with default includes and customized entry point
2017-09-16 9:14 ` Amos Bird
@ 2017-09-16 9:36 ` Thierry Banel
0 siblings, 0 replies; 10+ messages in thread
From: Thierry Banel @ 2017-09-16 9:36 UTC (permalink / raw)
To: emacs-orgmode
On 16/09/2017 11:14, Amos Bird wrote:
>
> Hmm, is it possible to customize babel's c++ backend to achieve this?
>
You mean, hiding the call to twoSum() somewhere? I am not aware of any
basic way to achieve that.
> I may also want a evaluation that submits the code to online judge and
> returns the result.
>
This seems to be another topic. One that Babel is not concerned about.
Are you trying to set up a work-flow where students submit their
solutions to a teacher who is the "judge"? Isn't email between students
and teacher just the simple and straightforward work-flow?
> Thierry Banel <tbanelwebmin@free.fr> writes:
>
> > You still need to explicitly call twoSum(), and that cannot be hidden.
> >
> > On 16/09/2017 10:58, Amos Bird wrote:
> >
> > Ok, i get the idea. So how can I customized the default code
> expansion so that it can directly evaluate this
> >
> > class Solution {
> > public:
> > vector<int> twoSum(vector<int>& nums, int target) {
> >
> > }
> > };
>
> –
> Amos Bird
> amosbird@gmail.com
>
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: evaluate cpp snippet in org babel with default includes and customized entry point
2017-09-16 8:54 ` Thierry Banel
2017-09-16 8:58 ` Amos Bird
@ 2017-09-16 9:05 ` Thierry Banel
1 sibling, 0 replies; 10+ messages in thread
From: Thierry Banel @ 2017-09-16 9:05 UTC (permalink / raw)
To: emacs-orgmode
You may further hide the includes with a property drawer. An additional
benefit is that those includes are shared among all babels under the
same section.
* Example
:PROPERTIES:
:includes: <iostream> <vector>
:END:
#+BEGIN_SRC C++
std::cout<<"hello";
#+END_SRC
#+RESULTS:
: hello
On 16/09/2017 10:54, Thierry Banel wrote:
> You may use the :includes header parameter.
>
> Also, you may omit the main() function. In this case, all your code
> will become the body of a default main function. Type C-c C-v v on the
> following example to understand what is happening.
>
>
> #+BEGIN_SRC C++ :includes <iostream>
> std::cout<<"hello";
> #+END_SRC
>
> #+RESULTS:
> : hello
>
>
> On 16/09/2017 10:37, Amos Bird wrote:
>>
>> Thanks Thierry,
>>
>> But I'd like to hide all those includes and the main function. I'm
>> not sure if it's possible.
>>
>>
>
>
>
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: evaluate cpp snippet in org babel with default includes and customized entry point
2017-09-16 7:50 evaluate cpp snippet in org babel with default includes and customized entry point Amos Bird
2017-09-16 8:33 ` Thierry Banel
@ 2017-09-16 8:46 ` Thierry Banel
1 sibling, 0 replies; 10+ messages in thread
From: Thierry Banel @ 2017-09-16 8:46 UTC (permalink / raw)
To: emacs-orgmode
Another starting point without hard-coding inputs:
#+name: NUMS
| 2 |
| 7 |
| 11 |
| 15 |
#+BEGIN_SRC C++ :flags -std=c++11 :var target=9 :var inputnums=NUMS
#include <vector>
#include <iostream>
using namespace std;
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> result;
result.push_back(0);
result.push_back(1);
return result;
}
};
int main()
{
Solution solution;
vector<int> nums;
for (int* in : inputnums)
nums.push_back(*in);
for (int x : solution.twoSum(nums,target))
cout<<x<<"\n";
}
#+END_SRC
#+RESULTS:
| 0 |
| 1 |
On 16/09/2017 09:50, Amos Bird wrote:
>
> Hi
>
> I'd like to write a blog about leetcode solutions in c++. How can I
> evaluate those c++ code snippet using org babel?
>
>
> 1 Array
>
>
> 1.1 Two Sum
>
> Given an array of integers, return indices of the two numbers such
> that they add up to a specific target.
>
> You may assume that each input would have exactly one solution, and
> you may not use the same element twice.
>
> Example:
> Given nums = [2, 7, 11, 15], target = 9,
>
> Because nums[0] + nums[1] = 2 + 7 = 9,
> return [0, 1].
>
> |class Solution {
> public:
> vector<int> twoSum(vector<int>&nums,int target) {
>
> }
> };
> |
>
> I need to add default headers and pass arguments to the entry class.
>
> –
> Amos Bird
> amosbird@gmail.com
>
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2017-09-16 9:37 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-09-16 7:50 evaluate cpp snippet in org babel with default includes and customized entry point Amos Bird
2017-09-16 8:33 ` Thierry Banel
2017-09-16 8:37 ` Amos Bird
2017-09-16 8:54 ` Thierry Banel
2017-09-16 8:58 ` Amos Bird
2017-09-16 9:11 ` Thierry Banel
2017-09-16 9:14 ` Amos Bird
2017-09-16 9:36 ` Thierry Banel
2017-09-16 9:05 ` Thierry Banel
2017-09-16 8:46 ` Thierry Banel
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).